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

Redefining Literal class variable in subclass has 'Incompatible type' #8796

Closed
AugPro opened this issue May 11, 2020 · 10 comments · Fixed by #13494
Closed

Redefining Literal class variable in subclass has 'Incompatible type' #8796

AugPro opened this issue May 11, 2020 · 10 comments · Fixed by #13494
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-literal-types

Comments

@AugPro
Copy link

AugPro commented May 11, 2020

Are you reporting a bug, or opening a feature request?

Bug

Please insert below the code you are checking with mypy,

or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.

from typing import Literal

class A:
    foo : Literal['bar', 'spam']= 'bar'
    
class B(A):
    foo = 'spam'

What is the actual behavior/output?

main.py:7: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "Union[Literal['bar'], Literal['spam']]")
Found 1 error in 1 file (checked 1 source file)

What is the behavior/output you expect?

I expected no error, as B.foo is still of type Literal['bar', 'spam']

What are the versions of mypy and Python you are using?

mypy == 0.770
Python == 3.8

What are the mypy flags you are using? (For example --strict-optional)

no flags

@msullivan msullivan added bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-literal-types labels May 18, 2020
@msullivan
Copy link
Collaborator

Yup, that looks like a bug. Does it work as a workaround if you repeat the type annotation in the subclass?

@AugPro
Copy link
Author

AugPro commented May 18, 2020

yes it does ! It also works if I overwrite foo in __init__ method for instance, and in that case no need to repeat type annotation

@peterschutt
Copy link

Is this the same issue?

from typing import Generic, List, Literal, TypeVar

T = TypeVar("T", bound=str)


class Thing(Generic[T]):
    collection: List[T] 
    word: T


class SubThing(Thing[Literal["word"]]):
    collection = ["word"]
    word = "word"
test.py:14: error: Incompatible types in assignment (expression has type "str", base class "Thing" defined the type as "Literal['word']")
Found 1 error in 1 file (checked 1 source file)

Mypy seems happy with the collection override but not word.

py == 3.8.5
mypy == 0.782

@embray
Copy link

embray commented May 3, 2021

Ditto with

$ mypy --version
mypy 0.812

and no additional flags.

@willfrey
Copy link

Just ran into this issue with 0.910.

@xsevy
Copy link

xsevy commented Jan 5, 2022

there's still the same problem 0.930

@cebasfu93
Copy link

Also ran into this bug with version 0.910

@amickael
Copy link

Is this fixed? Still running into this issue in 0.982

@JelleZijlstra
Copy link
Member

This was fixed in 57de8db which isn't in 0.982.

@amickael
Copy link

Ah makes sense, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-literal-types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants