blob: 3ed4c6d3d8e2ddcb30c11535d65d941bf58a325b [file] [log] [blame] [edit]
[case testNewSyntaxSyntaxError]
x: int: int # E: invalid syntax
[out]
[case testNewSyntaxBasics]
x: int
x = 5
y: int = 5
a: str
a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
b: str = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
zzz: int
zzz: str # E: Name "zzz" already defined on line 9
[out]
[case testNewSyntaxWithDict]
from typing import Dict, Any
d: Dict[int, str] = {}
d[42] = 'ab'
d[42] = 42 # E: Incompatible types in assignment (expression has type "int", target has type "str")
d['ab'] = 'ab' # E: Invalid index type "str" for "Dict[int, str]"; expected type "int"
[builtins fixtures/dict.pyi]
[out]
[case testNewSyntaxWithRevealType]
from typing import Dict
def tst_local(dct: Dict[int, T]) -> Dict[T, int]:
ret: Dict[T, int] = {}
return ret
reveal_type(tst_local({1: 'a'})) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
[builtins fixtures/dict.pyi]
[out]
[case testNewSyntaxWithInstanceVars]
class TstInstance:
a: str
def __init__(self) -> None:
self.x: int
TstInstance().x = 5
TstInstance().x = 'ab' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
TstInstance().a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
TstInstance().a = 'ab'
[out]
[case testNewSyntaxWithClassVars]
class CCC:
a: str = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
[out]
[case testNewSyntaxWithStrictOptional]
strict: int
strict = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
strict2: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]
[case testNewSyntaxWithStrictOptionalFunctions]
def f() -> None:
x: int
if int():
x = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]
[case testNewSyntaxWithStrictOptionalClasses]
class C:
def meth(self) -> None:
x: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
self.x: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]
[case testNewSyntaxSpecialAssign]
class X:
x: str
x[0]: int
x.x: int
[out]
main:3: error: Unexpected type declaration
main:3: error: Unsupported target for indexed assignment ("str")
main:4: error: Type cannot be declared in assignment to non-self attribute
main:4: error: "str" has no attribute "x"
[case testNewSyntaxFStringBasics]
f'foobar'
f'{"foobar"}'
f'foo{"bar"}'
f'.{1}.'
f'{type(1)}'
a: str
a = f'foobar'
a = f'{"foobar"}'
[builtins fixtures/f_string.pyi]
[case testNewSyntaxFStringExpressionsOk]
f'.{1 + 1}.'
f'.{1 + 1}.{"foo" + "bar"}'
[builtins fixtures/f_string.pyi]
[case testNewSyntaxFStringExpressionsErrors]
f'{1 + ""}'
f'.{1 + ""}'
[builtins fixtures/f_string.pyi]
[out]
main:1: error: Unsupported operand types for + ("int" and "str")
main:2: error: Unsupported operand types for + ("int" and "str")
[case testNewSyntaxFStringParseFormatOptions]
value = 10.5142
width = 10
precision = 4
f'result: {value:{width}.{precision}}'
[builtins fixtures/f_string.pyi]
[case testNewSyntaxFStringSingleField]
v = 1
reveal_type(f'{v}') # N: Revealed type is "builtins.str"
reveal_type(f'{1}') # N: Revealed type is "builtins.str"
[builtins fixtures/f_string.pyi]
[case testFeatureVersionSuggestion]
# flags: --python-version 3.99
x *** x this is what future python looks like public static void main String[] args await goto exit
[out]
main:2: error: invalid syntax; you likely need to run mypy using Python 3.99 or newer