Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work towards new semantic analyzer #6240

Merged
merged 13 commits into from
Jan 23, 2019
Merged

Initial work towards new semantic analyzer #6240

merged 13 commits into from
Jan 23, 2019

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jan 22, 2019

Implement basic deferred nodes functionality and remove most of old
pass 1 functionality. Also fork mypy.semanal_shared since I needed
to tweak the signature of anal_type in an incompatible way (there are
not other changes in that module).

A lot of things still don't work, including functions and more than two
passes. There are a small number of tests for the new semantic
analyzer, but I made no attempt at achieving good test coverage yet.

Work towards #6204.

@JukkaL JukkaL requested a review from ilevkivskyi January 22, 2019 17:11
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks like a good start, I have some optional comments.

@@ -181,7 +181,7 @@ def anal_type(self, t: Type, *,
allow_tuple_literal: bool = False,
allow_unbound_tvars: bool = False,
report_invalid_types: bool = True,
third_pass: bool = False) -> Type:
third_pass: bool = False) -> Optional[Type]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None means some names were nor resolved (type not ready), right? Maybe document this?

@@ -0,0 +1,85 @@
-- Test cases for the new semantic analyzer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should start gradually adding existing tests to the new semantic analyzer whitelist soon, instead of adding new tests. In few first PRs it is OK however.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can start thinking of this once basic stuff works.

[file a.py]
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add a test where the both modules import the same name (bad in this case)?

def semantic_analysis_for_scc(graph: 'Graph', scc: List[str]) -> None:
# Assume reachability analysis has already been performed.
process_top_levels(graph, scc)
process_functions(graph, scc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be more comfortable with a TODO here (for either implementation, or documenting that it is not supported) about global declarations in functions like this:

def f():
    global x
    x = 1
print(x)  # <- is this allowed?

deferred = [] # type: List[str]
while worklist:
next_id = worklist.pop()
deferred += semantic_analyze_target(next_id, graph[next_id])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion: Maybe we can add a simple abstraction layer for adding to/removing from the deferred queue? Currently this can be just a function calling set operations, but will be easier to refactor if we later will decide to switch to priority queues or similar.

targets = get_all_leaf_targets(symtable)
for target in targets:
deferred += semantic_analyze_target(target, graph[id])
assert not deferred # There can't be cross-function forward refs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a temporary limitation, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a limitation as such, since deferring functions should never be necessary. All namespaces need to complete before start processing functions.

info._fullname = self.qualified_name(defn.name)
self.globals[defn.name] = SymbolTableNode(GDEF, info)
else:
info._fullname = info.name()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume TODO about nested classes also applies here.

typ = t.accept(a)
if self.num_incomplete_refs != prev_incomplete:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way looks a bit tricky, maybe we can get this somehow from the return type (or use an exception)? This is fine for a follow-up PR if you agree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using return type may be tricky, since this needs to also work with the main AST visitor, which doesn't return anything. Using an exception may be a good idea. Let's revisit this later once we have a more complete implementation.

result.append(analyzed)
else:
# TODO: Is this the right thing to do?
result.append(AnyType(TypeOfAny.from_error))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just return Optional[List[Type]] from this function instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, it's unclear at this point. We can get back to this later.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jan 23, 2019

I'm going to merge this as-is since I have some other work almost ready that is built on top of this. I'll address some of the comments in another PR, and for others we can create follow-up tasks.

@JukkaL JukkaL merged commit 2195915 into master Jan 23, 2019
@JelleZijlstra JelleZijlstra deleted the new-semanal-proto branch January 23, 2019 16:32
JukkaL added a commit that referenced this pull request Jan 24, 2019
These respond to some review feedback to
#6240.
JukkaL added a commit that referenced this pull request Jan 24, 2019
These respond to some review feedback in #6240.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants