Skip to content

New semantic analyzer: refactor analysis of assignments #6412

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

Closed
ilevkivskyi opened this issue Feb 15, 2019 · 0 comments · Fixed by #6527
Closed

New semantic analyzer: refactor analysis of assignments #6412

ilevkivskyi opened this issue Feb 15, 2019 · 0 comments · Fixed by #6527
Assignees
Labels
priority-1-normal refactoring Changing mypy's internals semantic-analyzer Problems that happen during semantic analysis

Comments

@ilevkivskyi
Copy link
Member

This is a follow-up for #6390

Assignment statements can create a bunch of different symbol nodes:

  • Variable
  • Type alias
  • Type variable
  • Named tuple
  • Typed dict

Because of this current implementation is messy and has some hacks. We should refactor this to happen in two steps:

  • Classify the kind of assignment
  • Perform analysis accordingly for each kind

The tricky corner case is enums: c = Color['Red'] looks like a type alias, but this should create a variable.

@ilevkivskyi ilevkivskyi added refactoring Changing mypy's internals priority-1-normal semantic-analyzer Problems that happen during semantic analysis labels Feb 15, 2019
@ilevkivskyi ilevkivskyi self-assigned this Feb 15, 2019
ilevkivskyi added a commit that referenced this issue Mar 15, 2019
…rward (#6527)

Fixes #6412 
Fixes #6453 

In this PR:
* Wait until we can classify r.h.s. of an assignment as normal, type alias, named tuple etc.
* Choose the corresponding branch instead of performing all of them (as it used to be before)
* Never create (or rely on creation of) temporary `Var`s
* Add every symbol to symbol table only once (as it is done for classes)
* Don't "patch" existing symbols (with one exception for type variables, see below)
* Add and update docstrings and comments

Notes:
* I allow placeholders for type alias targets and NewTypes targets, they essentially behave as base classes. In general I make logic for type aliases and NewTypes much more similar to logic of class definitions (especially NewTypes), this allows to support a whole bunch of tricky forward references
* I don't store types with placeholders in annotations, and instead if an alias target got updated (e.g. placeholders are resolved), I set `progress=True`

Follow-ups:
* There is still a small hack to keep compatibility with old analyzer in type checker, but now it is basically "don't use binder for l.h.s. of special forms"
* Type variable declarations still have a bug that duplicate type variable definition is not reported as error, second one silently wins, this will be fixed in a separate PR
* This PR doesn't aim to fix aspects of assignment analysis related to #6422, that will be also a separate PR

Test updates:
* Add and enable more tests for type aliases and NewTypes
* Add test for cyclic definition error messages
* Change order of error messages on the same line in few tests
* Enable one test file where previously only some tests were opted in (I think it was not enabled by mistake in one of the previous PRs, I enable it here since order of errors in one test in that file is affected by this PR.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-1-normal refactoring Changing mypy's internals semantic-analyzer Problems that happen during semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant