Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
Please provide more information to help us understand the issue:
Are you reporting a bug, or opening a feature request?
Bug, assertionerror
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Running mypy --strict file.py crashes it but if you do normal run without strict and then strict it works until you do strict again. It seems doing strict off the bat without having ran without --strict previous crashes it.
Unfortunately I can't reproduce this, mypy just doesn't find the stubs (even though I installed from the right branch). It however looks like something bad (and it seems to me we had seen another similar report recently), so worth more investigation.
changed the title [-][Crash] Assertion error, importing library with typehints[/-][+]Assertion error, importing library with typehints[/+]on Mar 6, 2020
I can repro this crash, I needed to install aiohttp and websockets from requirements.txt for it to happen. It seems that the assertion failure happens for discord.channel.BaseUser. My guess is there a large import cycle in the channel/user pairing that is causing the issue, but I am not sure (I'm afraid I don't have more time to investigate this further).
I was able to fix this crash. webhook.pyi was importing discord.state.ConnectionState and this was causing the import cycles. I hadn't stubbed ConnectionState because it's not part of the public API, so I stubbed it as an opaque structure so Webhook.from_state() can use it. @Stonedestroyer, update your checkout to bryanforbes/discord.py@ea11c83 and let me know if the problem persists.
I did find an issue with an import cycle using from .channel import * in discord/__init__.pyi. I had to fix it by specifying exactly what to import from discord.channel (@Stonedestroyer you'll want to update your checkout to bryanforbes/discord.py@30fb161 ). This seems to have been introduced in 0.770.
@ethanhs I was able to reproduce the import cycle in the following gist: https://gist.github.com/bryanforbes/024ad256677d07faff0ed4dc5c5dd21a. In my reproduction project, I have a repro directory and I put all of those files into it. I then run .venv/bin/mypy repro/user.pyi and get the crash. If I change __init__.pyi to import all of the classes in channel.pyi individually (from .channel import TextChannel as TextChannel, ...), the crash goes away.
I am running into the same issue. I also suspect it is a kind of import cycle crash.
I have tried to reduce the example but by reducing it, the problem goes away. I also suspect some kind of import cycle because removing some module imports removes the problem.
You can easily reproduce it on my open source project :
$ mypy --no-implicit-reexport pkg/a.py
Traceback (most recent call last):
File "/home/rraval/.local/share/virtualenvs/encircle/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/home/rraval/.local/share/virtualenvs/encircle/lib/python3.6/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "mypy/main.py", line 90, in main
File "mypy/build.py", line 180, in build
File "mypy/build.py", line 254, in _build
File "mypy/build.py", line 2630, in dispatch
File "mypy/build.py", line 2953, in process_graph
File "mypy/build.py", line 3070, in process_stale_scc
File "mypy/build.py", line 2232, in write_cache
File "mypy/build.py", line 1445, in write_cache
File "mypy/nodes.py", line 303, in serialize
File "mypy/nodes.py", line 3098, in serialize
File "mypy/nodes.py", line 3034, in serialize
AssertionError
This seems to work:
$ mypy pkg/a.py
Success: no issues found in 1 source file
Version info:
$ mypy --version
mypy 0.790
My naive guess is that this is some interaction between --no-implicit-reexport and from pkg.a import *. Both seem to be necessary to make the assertion trigger occur.
I just ran into this issue with a large $work project as well, and we're sort of stuck with an older version of mypy that supports 2.7 and 3.8:
$ mypy --version
mypy 0.770
$
Do folks have any suggestions for how the import cycle can be tracked down other than maybe use --pdb (which wouldn't work with CI build processes)?
PS. I know this isn't the last version that supports both; this is just the version I standardized things on 1.5 years ago when I added it to our development environment.
@rraval thank you for the minimal reproduction, that is very helpful, I believe that the from pkg.a import * does indeed seem to be related to the cause.
@ngie-eign I would look for a patter such as an import cycle, where you do from module import *, where module conditionally imports some other thing with if TYPE_CHECKING.
Activity
ilevkivskyi commented on Mar 6, 2020
Unfortunately I can't reproduce this, mypy just doesn't find the stubs (even though I installed from the right branch). It however looks like something bad (and it seems to me we had seen another similar report recently), so worth more investigation.
bryanforbes commented on Mar 12, 2020
@ilevkivskyi let me know if you need help reproducing this. I'm the maintainer of the stubs and I'm experiencing the same issue on 0.770.
emmatyping commented on Mar 13, 2020
I can repro this crash, I needed to install aiohttp and websockets from requirements.txt for it to happen. It seems that the assertion failure happens for discord.channel.BaseUser. My guess is there a large import cycle in the channel/user pairing that is causing the issue, but I am not sure (I'm afraid I don't have more time to investigate this further).
bryanforbes commented on Apr 4, 2020
I was able to fix this crash. webhook.pyi was importing discord.state.ConnectionState and this was causing the import cycles. I hadn't stubbed ConnectionState because it's not part of the public API, so I stubbed it as an opaque structure so Webhook.from_state() can use it. @Stonedestroyer, update your checkout to bryanforbes/discord.py@ea11c83 and let me know if the problem persists.
bryanforbes commented on Apr 4, 2020
I did find an issue with an import cycle using from .channel import * in discord/__init__.pyi. I had to fix it by specifying exactly what to import from discord.channel (@Stonedestroyer you'll want to update your checkout to bryanforbes/discord.py@30fb161 ). This seems to have been introduced in 0.770.
emmatyping commented on Apr 5, 2020
@bryanforbes thank you for investigating this further! If you could find a more minimal reproduction that would be greatly appreciated.
bryanforbes commented on Apr 5, 2020
@ethanhs I'll see what I can do
bryanforbes commented on Apr 5, 2020
@ethanhs I was able to reproduce the import cycle in the following gist: https://gist.github.com/bryanforbes/024ad256677d07faff0ed4dc5c5dd21a. In my reproduction project, I have a repro directory and I put all of those files into it. I then run .venv/bin/mypy repro/user.pyi and get the crash. If I change __init__.pyi to import all of the classes in channel.pyi individually (from .channel import TextChannel as TextChannel, ...), the crash goes away.
emmatyping commented on Apr 6, 2020
@bryanforbes, thank you so much, that will be helpful in finding the source of the issue.
bluebird75 commented on Nov 22, 2020
I am running into the same issue. I also suspect it is a kind of import cycle crash.
I have tried to reduce the example but by reducing it, the problem goes away. I also suspect some kind of import cycle because removing some module imports removes the problem.
You can easily reproduce it on my open source project :
d:\work\winpdb\winpdb1>mypy --strict rpdb\rpc.py Traceback (most recent call last): File "c:\program files (x86)\python37-32\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "c:\program files (x86)\python37-32\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Program Files (x86)\Python37-32\Scripts\mypy.exe\__main__.py", line 7, in <module> File "c:\program files (x86)\python37-32\lib\site-packages\mypy\__main__.py", line 8, in console_entry main(None, sys.stdout, sys.stderr) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\main.py", line 90, in main res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 181, in build sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr, extra_plugins File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 254, in _build graph = dispatch(sources, manager, stdout) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2630, in dispatch process_graph(graph, manager) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2953, in process_graph process_stale_scc(graph, scc, manager) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 3070, in process_stale_scc graph[id].write_cache() File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 2236, in write_cache self.manager) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\build.py", line 1445, in write_cache data = tree.serialize() File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 303, in serialize 'names': self.names.serialize(self._fullname), File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 3098, in serialize data[key] = value.serialize(fullname, key) File "c:\program files (x86)\python37-32\lib\site-packages\mypy\nodes.py", line 3034, in serialize assert not isinstance(self.node, PlaceholderNode) AssertionErrorrraval commented on Dec 6, 2020
I managed to minimize the reproduction from https://github.com/bluebird75/winpdb, please see https://github.com/rraval/mypy-8481
This fails:
$ mypy --no-implicit-reexport pkg/a.py Traceback (most recent call last): File "/home/rraval/.local/share/virtualenvs/encircle/bin/mypy", line 8, in <module> sys.exit(console_entry()) File "/home/rraval/.local/share/virtualenvs/encircle/lib/python3.6/site-packages/mypy/__main__.py", line 8, in console_entry main(None, sys.stdout, sys.stderr) File "mypy/main.py", line 90, in main File "mypy/build.py", line 180, in build File "mypy/build.py", line 254, in _build File "mypy/build.py", line 2630, in dispatch File "mypy/build.py", line 2953, in process_graph File "mypy/build.py", line 3070, in process_stale_scc File "mypy/build.py", line 2232, in write_cache File "mypy/build.py", line 1445, in write_cache File "mypy/nodes.py", line 303, in serialize File "mypy/nodes.py", line 3098, in serialize File "mypy/nodes.py", line 3034, in serialize AssertionErrorThis seems to work:
Version info:
My naive guess is that this is some interaction between --no-implicit-reexport and from pkg.a import *. Both seem to be necessary to make the assertion trigger occur.
chore(github-actions): fix for python/mypy#8481
ngie-eign commented on Oct 10, 2021
I just ran into this issue with a large $work project as well, and we're sort of stuck with an older version of mypy that supports 2.7 and 3.8:
Do folks have any suggestions for how the import cycle can be tracked down other than maybe use --pdb (which wouldn't work with CI build processes)?
PS. I know this isn't the last version that supports both; this is just the version I standardized things on 1.5 years ago when I added it to our development environment.
emmatyping commented on Oct 11, 2021
@rraval thank you for the minimal reproduction, that is very helpful, I believe that the from pkg.a import * does indeed seem to be related to the cause.
@ngie-eign I would look for a patter such as an import cycle, where you do from module import *, where module conditionally imports some other thing with if TYPE_CHECKING.
ngie-eign commented on Oct 11, 2021
@ethanhs : thanks! I think I figured out the issue — it was environmental (unclean build; old cached files).
Fix crash involving explicit reexport, import cycle, wildcard (#11632)
Fix crash involving explicit reexport, import cycle, wildcard (#11632)
Fix crash involving explicit reexport, import cycle, wildcard (python…