From 98f8b96b1163f8c16dfa0157b801fa53c327e185 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 2 Aug 2020 14:02:26 -0700 Subject: [PATCH] Pass module-specific Options to fastparse.parse() Before this, per-module flags that are handled in fastparse (in particular no_implicit_optional) were seeing the flag's global value. Fixes #9208 --- mypy/build.py | 8 +++++--- test-data/unit/check-flags.test | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index f372f3d087a1..8d84196af642 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -762,13 +762,14 @@ def is_module(self, id: str) -> bool: """Is there a file in the file system corresponding to module id?""" return find_module_simple(id, self) is not None - def parse_file(self, id: str, path: str, source: str, ignore_errors: bool) -> MypyFile: + def parse_file(self, id: str, path: str, source: str, ignore_errors: bool, + options: Options) -> MypyFile: """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ t0 = time.time() - tree = parse(source, path, id, self.errors, options=self.options) + tree = parse(source, path, id, self.errors, options=options) tree._fullname = id self.add_stats(files_parsed=1, modules_parsed=int(not tree.is_stub), @@ -2001,7 +2002,8 @@ def parse_file(self) -> None: self.parse_inline_configuration(source) self.tree = manager.parse_file(self.id, self.xpath, source, - self.ignore_all or self.options.ignore_errors) + self.ignore_all or self.options.ignore_errors, + self.options) modules[self.id] = self.tree diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 37b2c2a92e68..0e23476c4d0e 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1501,3 +1501,32 @@ class ShouldNotBeFine(x): ... # E: Class cannot subclass 'x' (has type 'Any') disallow_subclassing_any = True \[mypy-m] disallow_subclassing_any = False + +[case testNoImplicitOptionalPerModule] +# flags: --config-file tmp/mypy.ini +import m + +[file m.py] +def f(a: str = None) -> int: + return 0 + +[file mypy.ini] +\[mypy] +no_implicit_optional = True +\[mypy-m] +no_implicit_optional = False + +[case testNoImplicitOptionalPerModulePython2] +# flags: --config-file tmp/mypy.ini --python-version 2.7 +import m + +[file m.py] +def f(a = None): + # type: (str) -> int + return 0 + +[file mypy.ini] +\[mypy] +no_implicit_optional = True +\[mypy-m] +no_implicit_optional = False