Skip to content

Crash with TypeVar with bound and @dataclass #10705

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
morph-dev opened this issue Jun 23, 2021 · 4 comments
Closed

Crash with TypeVar with bound and @dataclass #10705

morph-dev opened this issue Jun 23, 2021 · 4 comments
Labels

Comments

@morph-dev
Copy link

Crash Report
I experienced the crash when using some (I guess uncommon) combination of:

Traceback

generics_test.py:11: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.910
Traceback (most recent call last):
  File "mypy/semanal.py", line 4872, in accept
  File "mypy/nodes.py", line 687, in accept
  File "mypy/semanal.py", line 577, in visit_func_def
  File "mypy/semanal.py", line 609, in analyze_func_def
  File "mypy/semanal.py", line 4583, in defer
AssertionError: Must not defer during final iteration
generics_test.py:11: : note: use --pdb to drop into pdb

To Reproduce

from typing import TypeVar, Generic
from dataclasses import dataclass

T = TypeVar('T', bound='X')

@dataclass
class X:
    x: int

@dataclass
class A(Generic[T]):
    a: int

Few things that don't make it crash:

  • Moving T = TypeVar(...) after Class X:
  • Adding member of type T to class A

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9
  • Operating system and version: Ubuntu 20.04.2 LTS
@morp007
Copy link

morp007 commented Aug 9, 2021

Hello. Please, explain:

  1. why do you consider that this behavior is an error?
  2. why do you think that there should be no error?

As for me: I didn't see any error in that behavior in this code, because its strange, because there no forward declaration.

P.S. Sorry for my awful language, its not my native.

@morph-dev
Copy link
Author

morph-dev commented Aug 9, 2021

  1. why do you consider that this behavior is an error?

Well, I think that mypy shouldn't crash no matter the code.
It's fine to show error if code is not valid, but it shouldn't crash.

  1. why do you think that there should be no error?

As said above, it shouldn't crash, no matter the code.

This particular example is a bit weird, since type T is not used inside A, and I don't remember exactly why I needed it (my real use case was more complicated and here I posted minimal reproducible code).

Also, as said in the post, moving TypeVar after class X, makes everything fine, so it makes sense that everything should be fine in this specific order as well.

I hope this gives a bit more clarity.

@morp007
Copy link

morp007 commented Aug 12, 2021

Thanks for explanation. I definitely agree with this:

mypy shouldn't crash no matter the code

@hauntsaninja
Copy link
Collaborator

Thanks for reporting, duplicate of #10140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants