Skip to content

Generate an error if trying to use @final with TypedDict #7849

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
JukkaL opened this issue Nov 1, 2019 · 1 comment · Fixed by #7865
Closed

Generate an error if trying to use @final with TypedDict #7849

JukkaL opened this issue Nov 1, 2019 · 1 comment · Fixed by #7865

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 1, 2019

@final doesn't work as expected with TypedDicts, but mypy doesn't complain about it:

from typing_extensions import final, TypedDict

@final  # No error here
class C(TypedDict):
    x: float
    y: int
    z: str

class D(C):  # No error here
    pass

This is confusing, since users would expect that @final prevents structural subtyping, but that's not the case. The simplest fix would be to generate an error if @final is used in a TypedDict definition.

Hints: This should be a pretty simple thing to fix. Look at what happens in analyze_class in mypy/semanal.py.

Originally reported in #7845.

@TH3CHARLie
Copy link
Collaborator

@JukkaL I have submitted a PR #7865 , please take a look

ilevkivskyi pushed a commit that referenced this issue Nov 4, 2019
Fixes #7849 

This fix modifies function `analyze_class` in `mypy/semanal.py`, if the analyzer identifies a `TypedDict`, it then traverses all decorators and checks if `@final` exists, and if so, generates an error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants