google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.78k stars 279 forks source link

Fix resolution of ParamSpec in circular dependencies #1804

Closed copybara-service[bot] closed 1 month ago

copybara-service[bot] commented 1 month ago

Fix resolution of ParamSpec in circular dependencies

What's happening here?

  1. We load module bar, by requesting it from the loader.
  2. Before resolving types, we need to load its dependencies. So we load module foo.
  3. We'd like to load foo's dependencies, but that's bar and that's a circle, so we start resolving types in foo.
  4. When resolving external types we create alias an foo.bar, copying the unresolved signature from bar.bar.
  5. When resolving local types, _P is not resolvable within foo.
  6. We run visitors.AdjustTypeParameters, which seems to require local type resolution, and runs afterwards. It declares _P in foo. Now _P.args would be resolvable but we already finished resolving local types and we don't do it again.
  7. We resolve types in bar, validate bar's AST and return.
  8. We load module foo, by requesting it from the loader.
  9. It's cached. We just need to validate it. We realize that types aren't resolved, yet, e.g. _P.args. We used to do only external type resolution again, which did not resolve ParamSpec, and we eventually bailed. This change also adds local type resolution again, which does resolve ParamSpec.
frigus02 commented 1 month ago

This should fix #1797