Load newest grammar in face of a future grammar

This commit is contained in:
A5rocks
2025-08-21 13:22:28 +09:00
parent 23b1cdf73d
commit baa3c90d85
3 changed files with 24 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
import hashlib import hashlib
import os import os
import warnings
from typing import Generic, TypeVar, Union, Dict, Optional, Any from typing import Generic, TypeVar, Union, Dict, Optional, Any
from pathlib import Path from pathlib import Path
@@ -239,7 +240,15 @@ def load_grammar(*, version: str = None, path: str = None):
:param str version: A python version string, e.g. ``version='3.8'``. :param str version: A python version string, e.g. ``version='3.8'``.
:param str path: A path to a grammar file :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))
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( file = path or os.path.join(
'python', '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 # fine as long as we are using `clean_jedi_cache` as a session scoped
# fixture. # fixture.
usefixtures = clean_parso_cache usefixtures = clean_parso_cache
# Disallow warnings
filterwarnings = error

View File

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