{"id":6281,"library":"types-antlr4-python3-runtime","title":"Typing stubs for antlr4-python3-runtime","description":"This package provides static type checking stubs for the `antlr4-python3-runtime` library, enabling type checkers like MyPy and Pyright to analyze code that uses ANTLR 4's Python runtime. It is part of the `typeshed` project and aims to provide accurate annotations for `antlr4-python3-runtime==4.13.*`. Releases are frequent, often incorporating a date suffix, reflecting updates within the `typeshed` project and alignment with the underlying runtime.","status":"active","version":"4.13.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-hints","antlr","parser","lexer","grammar"],"install":[{"cmd":"pip install types-antlr4-python3-runtime","lang":"bash","label":"Install Stubs"},{"cmd":"pip install antlr4-python3-runtime","lang":"bash","label":"Install Runtime (Required)"}],"dependencies":[{"reason":"This package provides type stubs for the 'antlr4-python3-runtime' library; the actual runtime must be installed to execute code.","package":"antlr4-python3-runtime"},{"reason":"Requires Python 3.10 or newer.","package":"python","optional":false}],"imports":[{"symbol":"InputStream","correct":"from antlr4 import InputStream"},{"symbol":"CommonTokenStream","correct":"from antlr4 import CommonTokenStream"},{"symbol":"Lexer","correct":"from antlr4 import Lexer"},{"symbol":"Parser","correct":"from antlr4 import Parser"},{"symbol":"ParseTreeWalker","correct":"from antlr4.tree.Tree import ParseTreeWalker"},{"note":"Imports for lexer/parser classes generated by the ANTLR tool.","symbol":"GeneratedLexer","correct":"from MyGrammarLexer import MyGrammarLexer"}],"quickstart":{"code":"import os\nfrom antlr4 import InputStream, CommonTokenStream, ParseTreeWalker\n\n# --- Example: Assumes MyGrammar.g4 was compiled to MyGrammarLexer.py and MyGrammarParser.py ---\n# To generate these files, you need the ANTLR Java tool (antlr-VERSION-complete.jar):\n# java -jar /path/to/antlr-VERSION-complete.jar MyGrammar.g4 -Dlanguage=Python3\n\n# Placeholder for generated classes (replace with actual generated imports)\n# For a real scenario, these would be in files like MyGrammarLexer.py and MyGrammarParser.py\nclass MyGrammarLexer:\n    def __init__(self, input_stream):\n        pass # Simplified for quickstart\n    def getAllTokens(self):\n        return [] # Simplified\n\nclass MyGrammarParser:\n    def __init__(self, token_stream):\n        pass # Simplified\n    def myRule(self):\n        class Context:\n            def accept(self, visitor): pass\n            def toStringTree(self, recog=None): return \"(myRule)\"\n        return Context() # Simplified\n\nclass MyGrammarListener:\n    def enterMyRule(self, ctx): pass\n    def exitMyRule(self, ctx): pass\n\n# Example usage of the antlr4-python3-runtime with generated classes\ninput_text = \"hello world\"\ninput_stream = InputStream(input_text)\n\nlexer = MyGrammarLexer(input_stream)\ntoken_stream = CommonTokenStream(lexer)\nparser = MyGrammarParser(token_stream)\n\n# Assume 'myRule' is the entry point for your grammar\ntree = parser.myRule()\n\nprint(f\"Parsed tree: {tree.toStringTree(recog=parser)}\")\n\n# Using a listener for tree traversal\nlistener = MyGrammarListener()\nwalker = ParseTreeWalker()\nwalker.walk(listener, tree)\n\nprint(\"Type checking enabled for antlr4 classes by types-antlr4-python3-runtime.\")\n","lang":"python","description":"This quickstart demonstrates how to use the core `antlr4-python3-runtime` components with ANTLR-generated lexer and parser classes. The `types-antlr4-python3-runtime` package provides type hints for all `antlr4` imports and the expected interfaces of generated classes, improving developer experience with static analysis tools. Note that the ANTLR tool (a Java JAR) is required separately to generate `MyGrammarLexer.py` and `MyGrammarParser.py` from your `.g4` grammar file."},"warnings":[{"fix":"Upgrade `antlr4-python3-runtime` to version `4.13.0` or newer when using Python 3.13+.","message":"The `antlr4-python3-runtime` library, which these stubs target, has known incompatibilities with specific Python versions. Notably, `antlr4-python3-runtime==4.11.1` is incompatible with Python 3.13+ due to changes in CPython's internal APIs. Users of Python 3.13 or newer should ensure they are using `antlr4-python3-runtime>=4.13.0`. Always check the compatibility matrix for the underlying runtime.","severity":"breaking","affected_versions":"antlr4-python3-runtime < 4.13.0 for Python >= 3.13"},{"fix":"Ensure both `types-antlr4-python3-runtime` and `antlr4-python3-runtime` are installed. If generating parsers, ensure Java is installed and the ANTLR `.jar` tool is available.","message":"This package provides *only* type stubs. It does not include the actual `antlr4-python3-runtime` library. For your code to run, you must install `antlr4-python3-runtime` separately. Additionally, the ANTLR parser generator tool itself (which converts `.g4` grammars to Python source files) requires a Java Runtime Environment (JRE) to operate, which can be a point of confusion for Python-only developers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Report type-related issues or propose changes to the `antlr4-python3-runtime` stub in the main `typeshed` repository on GitHub.","message":"As a type stub package part of the `typeshed` project, fixes or improvements to the type annotations should be contributed directly to the `typeshed` GitHub repository, not to this `types-` package directly.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}