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

Fix crash from NamedTuple placeholder #18351

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Dec 28, 2024

Fixes #17396

I'm having trouble writing a regression test, but the following reproduces the issue nicely:

rm -rf repro
mkdir repro
echo 'from mod2 import UniqueAllResult' > repro/mod1.pyi
echo '
from typing import Generic, NamedTuple, TypeVar
from mod1 import does_not_exist

_SCT = TypeVar("_SCT", bound=does_not_exist)

class UniqueAllResult(NamedTuple, Generic[_SCT]):
    values: int
' > repro/mod2.pyi

MYPYPATH=repro mypy -c 'import mod1'

Fixes python#17396

I'm having trouble writing a regression test, but the following
reproduces the issue nicely:
```
rm -rf repro
mkdir repro
mkdir repro/np
echo 'from .arraysetops import UniqueAllResult' > repro/np/__init__.pyi
echo '
from typing import Generic, NamedTuple, TypeVar
from np import does_not_exist

_SCT = TypeVar("_SCT", bound=does_not_exist)

class UniqueAllResult(NamedTuple, Generic[_SCT]):
    values: int
' > repro/np/arraysetops.pyi
touch repro/np/py.typed

PYTHONPATH=repro mypy -c 'import np'
```
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit hacky, but I guess it may be a necessary evil. Two things:

  • Is there the same issue with generic TypedDicts? If yes, please fix that as well (and maybe try adding a test)
  • Can you verify that a pattern like this still works? (And maybe add a corresponding test if we don't have one):
T = TypeVar("T", bound="NT")
class NT(NamedTuple, Generic[T]):
    parent: T
    item: int

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Jan 1, 2025

Thanks for the review!

  • Yeah, I thought about this also but wasn't able to reproduce the issue with TypedDict
  • Good point, it no longer works. I opened Add regression test for NamedTuple with recursive bound #18399 for regression test, will figure out a way forward
  • I also realised why I can't repro in unit tests. There's some interaction with install location (so e.g. my original repro in PR comment may not even repro for you depending on how you have mypy installed

@hauntsaninja hauntsaninja marked this pull request as draft January 1, 2025 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mypy --follow-imports error or skip crashes with numpy 2.0.0
2 participants