Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Prevent crash on generic NamedTuple with unresolved typevar bound#18585

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

sterliakov
Copy link
Collaborator

Fixes#18582.Fixes#17396. Supersedes#18351.

cc@hauntsaninja - see my comments on your original PR, maybe this change should be lifted to BoolTypeQuery and TypeQuery instead? (opening a separate PR to see CI results)

@github-actionsGitHub Actions

This comment has been minimized.

@sterliakovsterliakov marked this pull request as ready for reviewFebruary 2, 2025 04:06
Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look, this makes sense. Let's merge this and try and get it into the release, since it's been reported multiple times.

I'm not sure about TypeQuery / BoolTypeQuery, probably makes sense to try? Would also be good to try and get a regression test working.

@hauntsaninjahauntsaninja mentioned this pull requestFeb 2, 2025
5 tasks
@ilevkivskyi
Copy link
Member

Nice catch! Yes, I think fixing this inTypeQuery/BoolTypeQuery is a more principled thing to do, it is also a bit more risky, but I think we should try it.

For more context,partial_fallback is an important part of the tuple types, not just some internal detail, this is essentially how we support named tuples, by constructing effectively an intersection of proper tuple type and an instance type. I guess it was an unintentional omission in the initial implementation of query visitors, as many other visitors actually do properly handle it.

@github-actionsGitHub Actions

This comment has been minimized.

@sterliakov
Copy link
CollaboratorAuthor

Both primer hits appear to reveal a different problem, reproducible as follows:

[case testTemp]foo = [    (1, ("a", "b")),    (2, []),][builtins fixtures/tuple.pyi]

The inferred type is:

builtins.list[tuple[builtins.int, typing.Sequence[builtins.str]]]

nothing suspicious, but...

(Pdb)  init_type.args[0].partial_fallbackbuiltins.tuple[Union[builtins.int, tuple[builtins.str, builtins.str], builtins.list[Never]], ...]

So when inferring list expr type as a constructor call, we synthetize a fully defined tuple with a partially unknown fallback (union of all initial values).

I think it's fine to keep for now and investigate separately?

And I'm still unable to write a testcase for the original problem, any suggestions are welcome!

@sterliakov
Copy link
CollaboratorAuthor

Alternatively, we can exclude fallback inInvalidInferredTypes visitor if that join behavior is correct and we just don't want to ask for explicit type for it. (or do we really want that and new behaviour is best?)

@ilevkivskyi
Copy link
Member

Alternatively, we can exclude fallback inInvalidInferredTypes visitor if that join behavior is correct and we just don't want to ask for explicit type for it.

This looks like a good short-term solution. Long term we may actually want to completely erase the fallback argument inTupleType constructor if fallback is plaintuple (everyone should usetypeops.tuple_fallback() anyway). I think however it may be better done in a separate PR.

Could you please skip fallback for now, and add a TODO there? (And maybe make a follow up PR if you have time)

@github-actionsGitHub Actions

This comment has been minimized.

@github-actionsGitHub Actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member

OK, so here is the repro for the crash (should be added to any of the incremental test files). The caveat is that repro requires#18592, as I didn't find any existing test fixture with aSelf type intuple MRO (as in real typeshed).

[case testSerializeDeferredGenericNamedTuple]import pkg[file pkg/__init__.py]from .lib import NT[file pkg/lib.py]from typing import Generic, NamedTuple, TypeVarfrom pkg import does_not_exist  # type: ignorefrom pkg.missing import also_missing  # type: ignoreT = TypeVar("T", bound=does_not_exist)class NT(NamedTuple, Generic[T]):    values: also_missing[T][file pkg/__init__.py.2]# touchfrom .lib import NT[builtins fixtures/tuple.pyi][out][out2]

JukkaL pushed a commit that referenced this pull requestFeb 3, 2025
This makes it more similar to the real typeshed. It is needed toreproduce tricky failures in tests, e.g.#18585. If this causes slower tests,some tests may be switched to `tuple-simple.pyi`.
@wesleywright
Copy link
Collaborator

Since this is a prominent crashing bug, I'll try to cherry pick it for the1.15 release if it lands in the next couple of days. If it's not ready by that point, I can prioritize cutting a quick 1.15.1 release to pick it up instead.

@sterliakov
Copy link
CollaboratorAuthor

Thank you@ilevkivskyi! I missed the Self-type relation. I verified that this testcase fails on current master and passes here.

@github-actionsGitHub Actions
Copy link
Contributor

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

@ilevkivskyiilevkivskyi merged commit237933a intopython:masterFeb 3, 2025
18 checks passed
wesleywright pushed a commit that referenced this pull requestFeb 4, 2025
This makes it more similar to the real typeshed. It is needed toreproduce tricky failures in tests, e.g.#18585. If this causes slower tests,some tests may be switched to `tuple-simple.pyi`.
wesleywright pushed a commit that referenced this pull requestFeb 4, 2025
…8585)Fixes#18582.Fixes#17396. Supersedes#18351.---------Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
wesleywright pushed a commit that referenced this pull requestFeb 4, 2025
…8585)Fixes#18582.Fixes#17396. Supersedes#18351.---------Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
wesleywright pushed a commit that referenced this pull requestFeb 4, 2025
This makes it more similar to the real typeshed. It is needed toreproduce tricky failures in tests, e.g.#18585. If this causes slower tests,some tests may be switched to `tuple-simple.pyi`.
ilevkivskyi added a commit that referenced this pull requestFeb 10, 2025
Removes todo added in#18585 - such normalization is not technicallycorrect when a tuple is used as a base class.---------Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull requestFeb 24, 2025
This makes it more similar to the real typeshed. It is needed toreproduce tricky failures in tests, e.g.python#18585. If this causes slower tests,some tests may be switched to `tuple-simple.pyi`.
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull requestFeb 24, 2025
…thon#18585)Fixespython#18582.Fixespython#17396. Supersedespython#18351.---------Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull requestFeb 24, 2025
Removes todo added inpython#18585 - such normalization is not technicallycorrect when a tuple is used as a base class.---------Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ilevkivskyiilevkivskyiilevkivskyi approved these changes

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Serialization crash onpandera withpandas-stubs mypy --follow-imports error or skip crashes with numpy 2.0.0
4 participants
@sterliakov@ilevkivskyi@wesleywright@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp