8000 fix fixtures and tests · python/mypy@b3b301f · GitHub
[go: up one dir, main page]

Skip to content

Commit b3b301f

Browse files
committed
fix fixtures and tests
1 parent 1bc2b9d commit b3b301f

15 files changed

+51
-20
lines changed

mypy/test/typefixture.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
from typing import List, Optional, Tuple
77

8+
from mypy.semanal_shared import set_callable_name
89
from mypy.types import (
910
Type, TypeVarType, AnyType, NoneType, Instance, CallableType, TypeVarDef, TypeType,
1011
UninhabitedType, TypeOfAny, TypeAliasType, UnionType, LiteralType
1112
)
1213
from mypy.nodes import (
13-
TypeInfo, ClassDef, Block, ARG_POS, ARG_OPT, ARG_STAR, SymbolTable,
14-
COVARIANT, TypeAlias
14+
TypeInfo, ClassDef, FuncDef, Block, ARG_POS, ARG_OPT, ARG_STAR, SymbolTable,
15+
COVARIANT, TypeAlias, SymbolTableNode, MDEF,
1516
)
1617

1718

@@ -62,8 +63,11 @@ def make_type_var(name: str, id: int, values: List[Type], upper_bound: Type,
6263
typevars=['T'],
6364
variances=[COVARIANT]) # class tuple
6465
self.type_typei = self.make_type_info('builtins.type') # class type
66+
self.bool_type_info = self.make_type_info('builtins.bool')
67+
self._add_bool_dunder(self.bool_type_info)
6568
self.functioni = self.make_type_info('builtins.function') # function TODO
6669
self.ai = self.make_type_info('A', mro=[self.oi]) # class A
70+
self._add_bool_dunder(self.ai)
6771
self.bi = self.make_type_info('B', mro=[self.ai, self.oi]) # class B(A)
6872
self.ci = self.make_type_info('C', mro=[self.ai, self.oi]) # class C(A)
6973
self.di = self.make_type_info('D', mro=[self.oi]) # class D
@@ -165,6 +169,12 @@ def make_type_var(name: str, id: int, values: List[Type], upper_bound: Type,
165169
self.type_t = TypeType.make_normalized(self.t)
166170
self.type_any = TypeType.make_normalized(self.anyt)
167171

172+
def _add_bool_dunder(self, type_info: TypeInfo) -> None:
173+
signature = CallableType([], [], [], Instance(self.bool_type_info, []), None)
174+
bool_func = FuncDef('__bool__', [], Block([]))
175+
bool_func.type = set_callable_name(signature, bool_func)
176+
type_info.names[bool_func.name] = SymbolTableNode(MDEF, bool_func)
177+
168178
# Helper methods
169179

170180
def callable(self, *a: Type) -> CallableType:

test-data/unit/check-dynamic-typing.test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ class C: pass
212212
[file builtins.py]
213213
class object:
214214
def __init__(self): pass
215-
class bool: pass
216-
class int: pass
215+
class int:
216+
def __bool__(self) -> bool: pass
217+
class bool(int): pass
217218
class type: pass
218219
class function: pass
219220
class str: pass

test-data/unit/check-enum.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,13 @@ else:
894894

895895
[case te B41A stEnumReachabilityWithNone]
896896
# flags: --strict-optional
897-
from enum import Enum
897+
from enum import Flag
898898
from typing import Optional
899899

900-
class Foo(Enum):
900+
class Foo(Flag):
901901
A = 1
902902
B = 2
903-
C = 3
903+
C = 4
904904

905905
x: Optional[Foo]
906906
if x:

test-data/unit/check-expressions.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ if int():
323323
b = b or a # E: Incompatible types in assignment (expression has type "Union[bool, A]", variable has type "bool")
324324
if int():
325325
b = a or b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool")
326-
class A: pass
326+
class A:
327+
def __bool__(self) -> bool: pass
327328

328329
[builtins fixtures/bool.pyi]
329330

@@ -375,8 +376,10 @@ if int():
375376
d = c or d # E: Incompatible types in assignment (expression has type "C", variable has type "D")
376377
if int():
377378
d = d or c # E: Incompatible types in assignment (expression has type "C", variable has type "D")
378-
class C: pass
379-
class D(C): pass
379+
class C:
380+
def __bool__(self) -> bool: pass
381+
class D(C):
382+
def __bool__(self) -> bool: pass
380383
[builtins fixtures/bool.pyi]
381384

382385
[case testInOperator]

test-data/unit/check-inference-context.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,10 @@ if int():
749749
if int():
750750
b = b or c # E: Incompatible types in assignment (expression has type "Union[List[B], List[C]]", variable has type "List[B]")
751751

752-
class A: pass
753-
class B: pass
752+
class A:
753+
def __bool__(self) -> bool: ...
754+
class B:
755+
def __bool__(self) -> bool: ...
754756
class C(B): pass
755757
[builtins fixtures/list.pyi]
756758

test-data/unit/check-literal.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,7 @@ q: Union[Truth, NoAnswerSpecified]
32943294
if q:
32953295
reveal_type(q) # N: Revealed type is "Union[__main__.Truth, __main__.NoAnswerSpecified]"
32963296
else:
3297-
reveal_type(q) # N: Revealed type is "__main__.NoAnswerSpecified"
3297+
reveal_type(q) # E: Statement is unreachable
32983298

32993299
w: Union[Truth, AlsoTruth]
33003300

test-data/unit/check-typevar-values.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ class B:
8686
def g(self) -> B: return B()
8787
AB = TypeVar('AB', A, B)
8888
def f(x: AB) -> AB:
89+
indeterminate: bool
8990
y = x
90-
if y:
91+
if indeterminate:
9192
return y.f()
9293
else:
9394
return y.g() # E: Incompatible return value type (got "B", expected "A")

test-data/unit/fixtures/bool.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class object:
1010
class type: pass
1111
class tuple(Generic[T]): pass
1212
class function: pass
13-
class int: pass
13+
class int:
14+
def __bool__(self) -> 'bool': pass
1415
class bool(int): pass
1516
class float: pass
16-
class str: pass
17+
class str:
18+
def __len__(self) -> int: pass
1719
class unicode: pass
1820
class ellipsis: pass

test-data/unit/fixtures/isinstancelist.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ def issubclass(x: object, t: Union[type, Tuple]) -> bool: pass
1818

1919
class int:
2020
def __add__(self, x: int) -> int: pass
21+
def __bool__(self) -> bool: pass
2122
class float: pass
2223
class bool(int): pass
2324
class str:
2425
def __add__(self, x: str) -> str: pass
2526
def __getitem__(self, x: int) -> str: pass
27+
def __len__(self) -> int: pass
2628

2729
T = TypeVar('T')
2830
KT = TypeVar('KT')

test-data/unit/fixtures/list.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class list(Sequence[T]):
1616
@overload
1717
def __init__(self, x: Iterable[T]) -> None: pass
1818
def __iter__(self) -> Iterator[T]: pass
19+
def __len__(self) -> int: pass
1920
def __contains__(self, item: object) -> bool: pass
2021
def __add__(self, x: list[T]) -> list[T]: pass
2122
def __mul__(self, x: int) -> list[T]: pass
@@ -26,7 +27,8 @@ class list(Sequence[T]):
2627

2728
class tuple(Generic[T]): pass
2829
class function: pass
29-
class int: pass
30+
class int:
31+
def __bool__(self) -> bool: pass
3032
class float: pass
3133
class str: pass
3234
class bool(int): pass

test-data/unit/fixtures/ops.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class tuple(Sequence[Tco]):
2424

2525
class function: pass
2626

27-
class bool: pass
27+
class bool(int): pass
2828

2929
class str:
3030
def __init__(self, x: 'int') -> None: pass
3131
def __add__(self, x: 'str') -> 'str': pass
3232
def __eq__(self, x: object) -> bool: pass
33+
def __len__(self) -> int: pass
3334
def startswith(self, x: 'str') -> bool: pass
3435
def strip(self) -> 'str': pass
3536

@@ -55,6 +56,7 @@ class int:
5556
def __le__(self, x: 'int') -> bool: pass
5657
def __gt__(self, x: 'int') -> bool: pass
5758
def __ge__(self, x: 'int') -> bool: pass
59+
def __bool__(self) -> bool: pass
5860

5961
class float:
6062
def __add__(self, x: 'float') -> 'float': pass

test-data/unit/fixtures/primitives.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class int:
1717
def __init__(self, x: object = ..., base: int = ...) -> None: pass
1818
def __add__(self, i: int) -> int: pass
1919
def __rmul__(self, x: int) -> int: pass
20+
def __bool__(self) -> bool: pass
2021
class float:
2122
def __float__(self) -> float: pass
2223
class complex: pass

test-data/unit/lib-stub/builtins.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ class type:
1111
# These are provided here for convenience.
1212
class int:
1313
def __add__(self, other: int) -> int: pass
14+
def __bool__(self) -> bool: pass
1415
class bool(int): pass
1516
class float: pass
1617

17-
class str: pass
18-
class bytes: pass
18+
class str:
19+
def __len__(self) -> int: pass
20+
class bytes:
21+
def __len__(self) -> int: pass
1922

2023
class function: pass
2124
class ellipsis: pass

test-da BBE7 ta/unit/lib-stub/enum.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def unique(enumeration: _T) -> _T: pass
3535

3636
class Flag(Enum):
3737
def __or__(self: _T, other: Union[int, _T]) -> _T: pass
38+
def __bool__(self) -> bool: ...
3839

3940

4041
class IntFlag(int, Flag):

test-data/unit/lib-stub/typing.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Generator(Iterator[T], Generic[T, U, V]):
4242

4343
class Sequence(Iterable[T_co]):
4444
def __getitem__(self, n: Any) -> T_co: pass
45+
def __len__(self) -> int: pass
4546

4647
class Mapping(Generic[T, T_co]): pass
4748

0 commit comments

Comments
 (0)
0