Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75b6d3c

Browse files
Don't error unnecessarily about new syntax in dependencies where we ignore errors anyways (#21883)
This fixes #21178 (at least my own root cause for it). This is actually a bit trickier than I thought (you can read that issue to see what I thought would work, which was naive!). However, even though we use the `ast` module and cannot make the errors nonblocking, we can work around the root cause of that issue i.e. that new features in dependencies can cause mypy to fail. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ab3849b commit 75b6d3c

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

mypy/fastparse.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ def parse(
206206
options = Options()
207207
errors.set_file(fnam, module, options=options)
208208
is_stub_file = fnam.endswith(".pyi")
209-
if is_stub_file:
209+
if ignore_errors:
210+
feature_version = sys.version_info[1]
211+
elif is_stub_file:
210212
feature_version = defaults.PYTHON3_VERSION[1]
211-
if options.python_version[0] == 3 and options.python_version[1] > feature_version:
212-
feature_version = options.python_version[1]
213213
else:
214214
assert options.python_version[0] >= 3
215215
feature_version = options.python_version[1]
216+
if options.python_version[0] == 3 and options.python_version[1] > feature_version:
217+
feature_version = options.python_version[1]
216218
try:
217219
# Disable
218220
# - deprecation warnings for 'invalid escape sequence' (Python 3.11 and below)

mypy/test/data.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ def _item_fail(msg: str) -> NoReturn:
158158
for arg in args:
159159
if arg.startswith("version"):
160160
compare_op = arg[7:9]
161-
if compare_op not in {">=", "=="}:
162-
_item_fail("Only >= and == version checks are currently supported")
161+
if compare_op not in {">=", "==", "< "}:
162+
_item_fail(
163+
"Only `>=', `==', and `< ' version checks are currently supported"
164+
)
163165
version_str = arg[9:]
164166
try:
165167
version = tuple(int(x) for x in version_str.split("."))
@@ -181,6 +183,12 @@ def _item_fail(msg: str) -> NoReturn:
181183
f'Only minor or patch version checks are currently supported with "==": {version_str!r}'
182184
)
183185
version_check = sys.version_info[: len(version)] == version
186+
elif compare_op == "< ":
187+
if version < defaults.PYTHON3_VERSION:
188+
_item_fail(
189+
f"{arg} always false since minimum runtime version is {defaults.PYTHON3_VERSION}"
190+
)
191+
version_check = sys.version_info < version
184192
if version_check:
185193
tmp_output = [expand_variables(line) for line in item.data]
186194
if os.path.sep == "\\" and case.normalize_output:

test-data/unit/check-inline-config.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,10 @@ x = 1 # type: ignore # E: Unused "type: ignore" comment
475475
x = 1 # type: ignore # E: Unused "type: ignore" comment
476476
[out]
477477
main:2: error: Unrecognized option: amongus = True
478+
479+
[case testNewSyntaxOldMypy]
480+
# flags: --python-version 3.10
481+
# mypy: ignore-errors=True
482+
x = t""
483+
[out version< 3.14]
484+
main:3: error: Invalid syntax

0 commit comments

Comments
 (0)