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

Unreplaced NamedType (pyi-error) #1229

Closed
romanngg opened this issue Jun 19, 2022 · 4 comments
Closed

Unreplaced NamedType (pyi-error) #1229

romanngg opened this issue Jun 19, 2022 · 4 comments
Assignees
Labels
bug cat: stubs and 3p type stubs and third-party types

Comments

@romanngg
Copy link

Per suggestion in https://google.github.io/pytype/errors.html#pyi-error filing the bug here:
https://github.com/google/neural-tangents/actions/runs/2522284898

Error is

[23/63] check neural_tangents.__init__
FAILED: /home/runner/work/neural-tangents/neural-tangents/.pytype/pyi/neural_tangents/__init__.pyi 
/opt/hostedtoolcache/Python/3.7.13/x64/bin/python -m pytype.single --imports_info /home/runner/work/neural-tangents/neural-tangents/.pytype/imports/neural_tangents.__init__.imports --module-name neural_tangents.__init__ --platform linux -V 3.7 -o /home/runner/work/neural-tangents/neural-tangents/.pytype/pyi/neural_tangents/__init__.pyi --analyze-annotated --nofail --quick /home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 22, in <module>: Couldn't import pyi for '' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 23, in <module>: Couldn't import pyi for '' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 25, in <module>: Couldn't import pyi for '_src.batching' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 27, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 28, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 29, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 30, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 31, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 32, in <module>: Couldn't import pyi for '_src.empirical' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 34, in <module>: Couldn't import pyi for '_src.monte_carlo' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
File "/home/runner/work/neural-tangents/neural-tangents/neural_tangents/__init__.py", line 36, in <module>: Couldn't import pyi for '' [pyi-error]
  Unreplaced NamedType: 'neural_tangents.predict', referenced from 'neural_tangents'
For more details, see https://google.github.io/pytype/errors.html#pyi-error

The change that cause it (google/neural-tangents@e28971c) was adding the new sub-module

from . import experimental

into the module init file
https://github.com/google/neural-tangents/blob/main/neural_tangents/__init__.py
Any suggestions what I might be doing wrong? Thanks a lot!

@rchen152 rchen152 added bug cat: stubs and 3p type stubs and third-party types labels Jun 21, 2022
@rchen152 rchen152 self-assigned this Jun 22, 2022
@rchen152
Copy link
Contributor

I haven't pinned down the exact cause yet, but it seems to be a circular dependency issue - I'm seeing pytype generate .pyi-1 (intermediate) stubs. Likely what happened is that adding the from . import experimental import created a cycle somewhere that our stub loader isn't doing enough dependency resolution passes to fully resolve, leaving an unresolved name behind.

Are you importing things from the neural_tangents/ submodules into __init__.py and then importing those names from __init__.py in other submodules? Concretely, I mean doing something like this:

# neural_tangents/foo.py
class Foo: ...
# neural_tangents/__init__.py
from .foo import *
# neural_tangents/bar.py
from . import Foo

If so, one possible workaround for circular dependency issues is to have submodules import directly from each other rather than from __init__.py. (So in this example, you'd change the import in bar.py to be from .foo import Foo.) That helps change the dependency graph from a cyclical one in which __init__.py depends on everything and everything depends on __init__.py to a more tree-like structure.

@romanngg
Copy link
Author

Thanks a lot Rachel, indeed that's exactly what I was doing without realizing! I'll fix it soon and get back to you to confirm.

@romanngg
Copy link
Author

Confirming - removing circular dependencies fixed the issue!

@rchen152
Copy link
Contributor

rchen152 commented Jul 1, 2022

Ok, I seem to have figured out half of the problem. Turns out the "Unreplaced NameType" errors were due to us not recognizing __init__.pyi-1 files as __init__ files, leading to package detection logic being messed up. That's easy enough to fix, thankfully. But after fixing that, there are still a few more errors:

File "/usr/local/google/home/rechen/neural-tangents/neural_tangents/experimental/__init__.py", line 15, in <module>: Couldn't import pyi for 'empirical_tf.empirical' [pyi-error]
  Can't find pyi for 'nt._src.utils.typing', referenced from 'neural_tangents.experimental.empirical_tf.empirical'
File "/usr/local/google/home/rechen/neural-tangents/neural_tangents/experimental/__init__.py", line 16, in <module>: Couldn't import pyi for 'empirical_tf.empirical' [pyi-error]
  Can't find pyi for 'nt._src.utils.typing', referenced from 'neural_tangents.experimental.empirical_tf.empirical'

I'll first get the fix for __init__.pyi-1 submitted, then try to figure out what's going on with the missing pyi files.

rchen152 added a commit that referenced this issue Jul 18, 2022
I also realized that the last time I changed is_package, I accidentally added
an extra '.' in the .pickled extension for __init__ files (so they became
__init__..pickled), but I did this both when unpickling and when checking the
filename of the unpickled file, so it worked itself out. This has been fixed.

For #1229.

PiperOrigin-RevId: 460546845
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: stubs and 3p type stubs and third-party types
Projects
None yet
Development

No branches or pull requests

2 participants