Merge pull request #234 from A5rocks/future-compatibility

Load newest grammar in face of a future grammar
This commit is contained in:
Dave Halter
2025-08-23 15:09:10 +00:00
committed by GitHub
3 changed files with 23 additions and 6 deletions

View File

@@ -239,7 +239,16 @@ def load_grammar(*, version: str = None, path: str = None):
:param str version: A python version string, e.g. ``version='3.8'``.
:param str path: A path to a grammar file
"""
version_info = parse_version_string(version)
# NOTE: this (3, 14) should be updated to the latest version parso supports.
# (if this doesn't happen, users will get older syntaxes and spurious warnings)
passed_version_info = parse_version_string(version)
version_info = min(passed_version_info, PythonVersionInfo(3, 14))
# # NOTE: this is commented out until parso properly supports newer Python grammars.
# if passed_version_info != version_info:
# warnings.warn('parso does not support %s.%s yet.' % (
# passed_version_info.major, passed_version_info.minor
# ))
file = path or os.path.join(
'python',

View File

@@ -10,3 +10,6 @@ norecursedirs = .* docs scripts normalizer_issue_files build
# fine as long as we are using `clean_jedi_cache` as a session scoped
# fixture.
usefixtures = clean_parso_cache
# Disallow warnings
filterwarnings = error

View File

@@ -4,15 +4,20 @@ from parso import utils
def test_load_inexisting_grammar():
# This version shouldn't be out for a while, but if we ever do, wow!
with pytest.raises(NotImplementedError):
load_grammar(version='15.8')
# The same is true for very old grammars (even though this is probably not
# going to be an issue.
# We support future grammars assuming future compatibility,
# but we don't know how to parse old grammars.
with pytest.raises(NotImplementedError):
load_grammar(version='1.5')
def test_load_grammar_uses_older_syntax():
load_grammar(version='4.0')
def test_load_grammar_doesnt_warn(each_version):
load_grammar(version=each_version)
@pytest.mark.parametrize(('string', 'result'), [
('2', (2, 7)), ('3', (3, 6)), ('1.1', (1, 1)), ('1.1.1', (1, 1)), ('300.1.31', (300, 1))
])