From a395e857897a85ed2e0417746cad11acb145d68c Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 23 Jan 2019 17:15:09 +0000 Subject: [PATCH 1/2] New semantic analyzer: Various small updates These respond to some review feedback to https://github.com/python/mypy/pull/6240. --- mypy/newsemanal/semanal.py | 2 +- mypy/newsemanal/semanal_main.py | 3 +++ mypy/plugin.py | 7 ++++++- test-data/unit/check-newsemanal.test | 8 ++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py index 9c8bff7a335d0..6602d5dd3b93c 100644 --- a/mypy/newsemanal/semanal.py +++ b/mypy/newsemanal/semanal.py @@ -2640,7 +2640,7 @@ def analyze_types(self, items: List[Expression]) -> List[Type]: if analyzed is not None: result.append(analyzed) else: - # TODO: Is this the right thing to do? + # TODO: Is this the right thing to do? Or maybe return Optional[List[Type]]? result.append(AnyType(TypeOfAny.from_error)) except TypeTranslationError: self.fail('Type expected', node) diff --git a/mypy/newsemanal/semanal_main.py b/mypy/newsemanal/semanal_main.py index 00fed2fef97b5..21fcf463cba52 100644 --- a/mypy/newsemanal/semanal_main.py +++ b/mypy/newsemanal/semanal_main.py @@ -17,6 +17,9 @@ def semantic_analysis_for_scc(graph: 'Graph', scc: List[str]) -> None: # Assume reachability analysis has already been performed. + # + # Note that functions can't define new module-level attributes, + # since module top levels are fully processed before functions. process_top_levels(graph, scc) process_functions(graph, scc) diff --git a/mypy/plugin.py b/mypy/plugin.py index fba43a29d2aac..23fe3c68b51a7 100644 --- a/mypy/plugin.py +++ b/mypy/plugin.py @@ -182,7 +182,12 @@ def anal_type(self, t: Type, *, allow_unbound_tvars: bool = False, report_invalid_types: bool = True, third_pass: bool = False) -> Optional[Type]: - """Analyze an unbound type.""" + """Analyze an unbound type. + + Return None if the some part of the type is not ready yet (only + happens with the new semantic analyzer). In this case the current + target being analyzed will be deferred and analyzed again. + """ raise NotImplementedError @abstractmethod diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index 25d3b667cf871..5e28a9fb94cca 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -80,6 +80,14 @@ from b import bad # E: Module 'b' has no attribute 'bad' [file b.py] from a import bad2 # E: Module 'a' has no attribute 'bad2' +[case testNewAnalyzerTypeAnnotationCycle4] +# flags: --new-semantic-analyzer +import b +[file a.py] +from b import bad # E: Module 'b' has no attribute 'bad' +[file b.py] +from a import bad + [case testNewAnalyzerSimpleFunction] # flags: --new-semantic-analyzer def f(x: int) -> str: From 6333811dc25313aaf607eea6879fa6a7375f33f3 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 24 Jan 2019 14:38:02 +0000 Subject: [PATCH 2/2] Updated based on feedback --- mypy/newsemanal/semanal_main.py | 11 +++++++---- test-data/unit/check-newsemanal.test | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mypy/newsemanal/semanal_main.py b/mypy/newsemanal/semanal_main.py index 21fcf463cba52..a113da67927f0 100644 --- a/mypy/newsemanal/semanal_main.py +++ b/mypy/newsemanal/semanal_main.py @@ -16,10 +16,13 @@ def semantic_analysis_for_scc(graph: 'Graph', scc: List[str]) -> None: - # Assume reachability analysis has already been performed. - # - # Note that functions can't define new module-level attributes, - # since module top levels are fully processed before functions. + """Perform semantic analysis for all modules in a SCC (import cycle). + + Assume that reachability analysis has already been performed. + """ + # Note that functions can't define new module-level attributes + # using 'global x', since module top levels are fully processed + # before functions. This limitation is unlikely to go away soon. process_top_levels(graph, scc) process_functions(graph, scc) diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index 5e28a9fb94cca..cb11e3e6ffabc 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -86,6 +86,7 @@ import b [file a.py] from b import bad # E: Module 'b' has no attribute 'bad' [file b.py] +# TODO: Could we generate an error here as well? from a import bad [case testNewAnalyzerSimpleFunction]