Skip to content

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

Closed
@AugPro

Description

@AugPro

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions