Skip to content

Commit 21e160e

Browse files
authored
Pass module-specific Options to fastparse.parse() (#9247)
Before this, per-module flags that are handled in fastparse (in particular no_implicit_optional) were seeing the flag's global value. Fixes #9208.
1 parent 274a9e8 commit 21e160e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

mypy/build.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,14 @@ def is_module(self, id: str) -> bool:
762762
"""Is there a file in the file system corresponding to module id?"""
763763
return find_module_simple(id, self) is not None
764764

765-
def parse_file(self, id: str, path: str, source: str, ignore_errors: bool) -> MypyFile:
765+
def parse_file(self, id: str, path: str, source: str, ignore_errors: bool,
766+
options: Options) -> MypyFile:
766767
"""Parse the source of a file with the given name.
767768
768769
Raise CompileError if there is a parse error.
769770
"""
770771
t0 = time.time()
771-
tree = parse(source, path, id, self.errors, options=self.options)
772+
tree = parse(source, path, id, self.errors, options=options)
772773
tree._fullname = id
773774
self.add_stats(files_parsed=1,
774775
modules_parsed=int(not tree.is_stub),
@@ -2001,7 +2002,8 @@ def parse_file(self) -> None:
20012002

20022003
self.parse_inline_configuration(source)
20032004
self.tree = manager.parse_file(self.id, self.xpath, source,
2004-
self.ignore_all or self.options.ignore_errors)
2005+
self.ignore_all or self.options.ignore_errors,
2006+
self.options)
20052007

20062008
modules[self.id] = self.tree
20072009

test-data/unit/check-flags.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,32 @@ class ShouldNotBeFine(x): ... # E: Class cannot subclass 'x' (has type 'Any')
15011501
disallow_subclassing_any = True
15021502
\[mypy-m]
15031503
disallow_subclassing_any = False
1504+
1505+
[case testNoImplicitOptionalPerModule]
1506+
# flags: --config-file tmp/mypy.ini
1507+
import m
1508+
1509+
[file m.py]
1510+
def f(a: str = None) -> int:
1511+
return 0
1512+
1513+
[file mypy.ini]
1514+
\[mypy]
1515+
no_implicit_optional = True
1516+
\[mypy-m]
1517+
no_implicit_optional = False
1518+
1519+
[case testNoImplicitOptionalPerModulePython2]
1520+
# flags: --config-file tmp/mypy.ini --python-version 2.7
1521+
import m
1522+
1523+
[file m.py]
1524+
def f(a = None):
1525+
# type: (str) -> int
1526+
return 0
1527+
1528+
[file mypy.ini]
1529+
\[mypy]
1530+
no_implicit_optional = True
1531+
\[mypy-m]
1532+
no_implicit_optional = False

0 commit comments

Comments
 (0)