blob: 3ae2df55f120d7bf69d9bb20605f2785f6d9abc0 [file]
-- Test cases for type checking mypy programs using full stubs and running
-- using CPython.
--
-- These are mostly regression tests -- no attempt is made to make these
-- complete.
--
-- This test file checks Enum
[case testEnumBasics]
from enum import Enum
class Medal(Enum):
gold = 1
silver = 2
bronze = 3
m = Medal.gold
m = 1
[out]
_program.py:7: error: Incompatible types in assignment (expression has type "int", variable has type "Medal")
[case testEnumNameAndValue]
from enum import Enum
class Truth(Enum):
true = True
false = False
x = ''
x = Truth.true.name
print(Truth.true.name)
print(Truth.false.value)
[out]
true
False
[case testEnumUnique]
import enum
@enum.unique
class E(enum.Enum):
x = 1
y = 1 # NOTE: This duplicate value is not detected by mypy at the moment
x = 1
x = E.x
[out]
_program.py:7: error: Incompatible types in assignment (expression has type "E", variable has type "int")
[case testIntEnum_assignToIntVariable]
from enum import IntEnum
class N(IntEnum):
x = 1
y = 1
n = 1
n = N.x # Subclass of int, so it's okay
s = ''
s = N.y
[out]
_program.py:8: error: Incompatible types in assignment (expression has type "N", variable has type "str")
[case testIntEnum_functionTakingIntEnum]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def takes_some_int_enum(n: SomeIntEnum):
pass
takes_some_int_enum(SomeIntEnum.x)
takes_some_int_enum(1) # Error
takes_some_int_enum(SomeIntEnum(1)) # How to deal with the above
[out]
_program.py:7: error: Argument 1 to "takes_some_int_enum" has incompatible type "int"; expected "SomeIntEnum"
[case testIntEnum_functionTakingInt]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def takes_int(i: int):
pass
takes_int(SomeIntEnum.x)
takes_int(2)
[case testIntEnum_functionReturningIntEnum]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def returns_some_int_enum() -> SomeIntEnum:
return SomeIntEnum.x
an_int = 1
an_int = returns_some_int_enum()
an_enum = SomeIntEnum.x
an_enum = returns_some_int_enum()
[out]
[case testEnumMethods]
from enum import Enum
class Color(Enum):
red = 1
green = 2
def m(self, x: int): pass
@staticmethod
def m2(x: int): pass
Color.red.m('')
Color.m2('')
[out]
_program.py:11: error: Argument 1 to "m" of "Color" has incompatible type "str"; expected "int"
_program.py:12: error: Argument 1 to "m2" of "Color" has incompatible type "str"; expected "int"
[case testIntEnum_ExtendedIntEnum_functionTakingExtendedIntEnum]
from enum import IntEnum
class ExtendedIntEnum(IntEnum):
pass
class SomeExtIntEnum(ExtendedIntEnum):
x = 1
def takes_int(i: int):
pass
takes_int(SomeExtIntEnum.x)
def takes_some_ext_int_enum(s: SomeExtIntEnum):
pass
takes_some_ext_int_enum(SomeExtIntEnum.x)
[case testNamedTupleEnum]
from typing import NamedTuple
from enum import Enum
N = NamedTuple('N', [('bar', int)])
class E(N, Enum):
X = N(1)
def f(x: E) -> None: pass
f(E.X)