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

(🐞) classmethod on TypedDict leads to "AssertionError: fallback can't be filled out until semanal" with --disallow-any-expr #13066

Closed
KotlinIsland opened this issue Jul 4, 2022 · 6 comments · Fixed by #13963
Labels
crash topic-disallow-any The disallow-any-* family of flags topic-typed-dict

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jul 4, 2022

from typing import TypedDict

class Foo(TypedDict):
    @classmethod  # E: AssertionError: fallback can't be filled out until semanal
    def baz(cls) -> "Foo": ...

This happens due to the Any expression (no-any-expr) trying to report an error.

@KotlinIsland KotlinIsland added the bug mypy got something wrong label Jul 4, 2022
@KotlinIsland
Copy link
Contributor Author

Oops, I was supposed to raise this as crash

@AlexWaygood AlexWaygood added crash topic-typed-dict and removed bug mypy got something wrong labels Jul 4, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 4, 2022

To be clear, this snippet only causes mypy to crash if the --disallow-any-expr option is enabled. If the option is not enabled, everything's fine.

@AlexWaygood AlexWaygood added the topic-disallow-any The disallow-any-* family of flags label Jul 4, 2022
@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Jul 4, 2022

Yes, but who in their right mind would ever not have that enabled. Also it's enabled by default in basedmypy.

@AlexWaygood
Copy link
Member

It's a legitimate crash report (and all crashes are v bad), I'm just laying out the steps needed to reproduce.

@ilevkivskyi
Copy link
Member

On master this doesn't crash, but gives a reasonable error

test.py:4: error: Invalid statement in TypedDict definition; expected "field_name: field_type"  [misc]

@ilevkivskyi
Copy link
Member

Oh, wait, I missed the flag.

@ilevkivskyi ilevkivskyi reopened this Oct 10, 2022
@JelleZijlstra JelleZijlstra changed the title (🐞) classmethod on TypedDict leads to "AssertionError: fallback can't be filled out until semanal" (🐞) classmethod on TypedDict leads to "AssertionError: fallback can't be filled out until semanal" with --disallow-any-expr Oct 10, 2022
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Oct 29, 2022
Fixes python#13066

During the semanal phase, mypy opts to ignore and skip processing
any malformed or illegal statements inside of a TypedDict class
definition, such as method definitions.

Skipping semanal analysis on these statements can cause any number
of odd downstream problems: the type-checking phase assumes that all
semanal-only semantic constructs (e.g. FakeInfo) have been purged
by this point, and so can crash at any point once this precondition has
been violated.

This diff opts to solve this problem by filtering down the list of
statements so we keep only the ones we know are legal within a TypedDict
definition.

The other possible solution to this problem is to modify mypy so we
skip checking TypedDict class bodies entirely during type checking and
fine-grained deps analysis. Doing this would also let address python#10007
and supersede my other diff python#13732.

I decided against doing this for now because:

1. I wasn't sure if this was actually safe, especially in the
   fine-grained deps phase and for mypyc.
2. I think no matter what, the semanal phase should not leak
   semanal-only types: relaxing this postcondition would make
   it harder to reason about mypy. So, we'd probably want to make this
   change regardless of what we do in the later phases.
JukkaL pushed a commit that referenced this issue Oct 31, 2022
Fixes #13066

During the semanal phase, mypy opts to ignore and skip processing any
malformed or illegal statements inside of a TypedDict class definition,
such as method definitions.

Skipping semanal analysis on these statements can cause any number of
odd downstream problems: the type-checking phase assumes that all
semanal-only semantic constructs (e.g. FakeInfo) have been purged by
this point, and so can crash at any point once this precondition has
been violated.

This diff opts to solve this problem by filtering down the list of
statements so we keep only the ones we know are legal within a TypedDict
definition.

The other possible solution to this problem is to modify mypy so we skip
checking TypedDict class bodies entirely during type checking and
fine-grained deps analysis. Doing this would also let address #10007 and
supersede my other diff #13732.

I decided against doing this for now because:

1. I wasn't sure if this was actually safe, especially in the
fine-grained deps phase and for mypyc.
2. I think no matter what, the semanal phase should not leak
semanal-only types: relaxing this postcondition would make it harder to
reason about mypy. So, we'd probably want to make this change regardless
of what we do in the later phases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-disallow-any The disallow-any-* family of flags topic-typed-dict
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants