Skip to content

Commit 1c74d33

Browse files
Merge branch 'master' into truedictprimitive
2 parents 9b5f28f + cb77a9b commit 1c74d33

File tree

8 files changed

+60
-8
lines changed

8 files changed

+60
-8
lines changed

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5332,13 +5332,15 @@ def visit_expression_stmt(self, s: ExpressionStmt) -> None:
53325332
s.expr.accept(self)
53335333

53345334
def visit_return_stmt(self, s: ReturnStmt) -> None:
5335+
old = self.statement
53355336
self.statement = s
53365337
if not self.is_func_scope():
53375338
self.fail('"return" outside function', s)
53385339
if self.return_stmt_inside_except_star_block:
53395340
self.fail('"return" not allowed in except* block', s, serious=True)
53405341
if s.expr:
53415342
s.expr.accept(self)
5343+
self.statement = old
53425344

53435345
def visit_raise_stmt(self, s: RaiseStmt) -> None:
53445346
self.statement = s

mypy/test/testsolve.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ def test_multiple_variables(self) -> None:
6464
)
6565

6666
def test_no_constraints_for_var(self) -> None:
67-
self.assert_solve([self.fx.t], [], [self.fx.uninhabited])
68-
self.assert_solve([self.fx.t, self.fx.s], [], [self.fx.uninhabited, self.fx.uninhabited])
67+
self.assert_solve([self.fx.t], [], [self.fx.a_uninhabited])
68+
self.assert_solve(
69+
[self.fx.t, self.fx.s], [], [self.fx.a_uninhabited, self.fx.a_uninhabited]
70+
)
6971
self.assert_solve(
7072
[self.fx.t, self.fx.s],
7173
[self.supc(self.fx.s, self.fx.a)],
72-
[self.fx.uninhabited, self.fx.a],
74+
[self.fx.a_uninhabited, self.fx.a],
7375
)
7476

7577
def test_simple_constraints_with_dynamic_type(self) -> None:
@@ -116,7 +118,7 @@ def test_poly_no_constraints(self) -> None:
116118
self.assert_solve(
117119
[self.fx.t, self.fx.u],
118120
[],
119-
[self.fx.uninhabited, self.fx.uninhabited],
121+
[self.fx.a_uninhabited, self.fx.a_uninhabited],
120122
allow_polymorphic=True,
121123
)
122124

@@ -152,7 +154,7 @@ def test_poly_free_pair_with_bounds_uninhabited(self) -> None:
152154
self.assert_solve(
153155
[self.fx.ub, self.fx.uc],
154156
[self.subc(self.fx.ub, self.fx.uc)],
155-
[self.fx.uninhabited, self.fx.uninhabited],
157+
[self.fx.a_uninhabited, self.fx.a_uninhabited],
156158
[],
157159
allow_polymorphic=True,
158160
)

mypy/test/typefixture.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def make_type_var(
7878
self.anyt = AnyType(TypeOfAny.special_form)
7979
self.nonet = NoneType()
8080
self.uninhabited = UninhabitedType()
81+
self.a_uninhabited = UninhabitedType()
82+
self.a_uninhabited.ambiguous = True
8183

8284
# Abstract class TypeInfos
8385

mypy/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,10 @@ def accept(self, visitor: TypeVisitor[T]) -> T:
12361236
return visitor.visit_uninhabited_type(self)
12371237

12381238
def __hash__(self) -> int:
1239-
return hash(UninhabitedType)
1239+
return hash((UninhabitedType, self.ambiguous))
12401240

12411241
def __eq__(self, other: object) -> bool:
1242-
return isinstance(other, UninhabitedType)
1242+
return isinstance(other, UninhabitedType) and other.ambiguous == self.ambiguous
12431243

12441244
def serialize(self) -> JsonDict:
12451245
return {".class": "UninhabitedType"}

mypyc/primitives/dict_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@
130130
error_kind=ERR_NEG_INT,
131131
)
132132

133+
# dict[key] = value (exact dict only, no subclasses)
134+
# NOTE: this is currently for internal use only, and not used for CallExpr specialization
135+
exact_dict_set_item_op = custom_op(
136+
arg_types=[dict_rprimitive, object_rprimitive, object_rprimitive],
137+
return_type=c_int_rprimitive,
138+
c_function_name="PyDict_SetItem",
139+
error_kind=ERR_NEG_INT,
140+
)
141+
133142
# key in dict
134143
binary_op(
135144
name="in",

test-data/unit/check-classes.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9258,3 +9258,18 @@ main:5: note: def __eq__(self, other: object) -> bool:
92589258
main:5: note: if not isinstance(other, C):
92599259
main:5: note: return NotImplemented
92609260
main:5: note: return <logic to compare two C instances>
9261+
9262+
[case testLambdaInAttributeCallValue]
9263+
# https://github.com/python/mypy/issues/19632
9264+
import foo
9265+
9266+
def nop(fn: object) -> foo.Bar:
9267+
return foo.Bar()
9268+
9269+
class Bar:
9270+
foo: foo.Bar = nop(
9271+
lambda: 0
9272+
)
9273+
[file foo.py]
9274+
class Bar:
9275+
...

test-data/unit/check-generic-subtyping.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,20 @@ s, s = Nums() # E: Incompatible types in assignment (expression has type "int",
753753
[builtins fixtures/for.pyi]
754754
[out]
755755

756+
[case testUninhabitedCacheChecksAmbiguous]
757+
# https://github.com/python/mypy/issues/19641
758+
from typing import Mapping, Never, TypeVar
759+
760+
M = TypeVar("M", bound=Mapping[str,object])
761+
762+
def get(arg: M, /) -> M:
763+
return arg
764+
765+
get({})
766+
767+
def upcast(d: dict[Never, Never]) -> Mapping[str, object]:
768+
return d # E: Incompatible return value type (got "dict[Never, Never]", expected "Mapping[str, object]")
769+
[builtins fixtures/dict.pyi]
756770

757771
-- Variance
758772
-- --------

test-data/unit/check-inference.test

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3781,10 +3781,18 @@ from typing import Any, Dict, NoReturn, NoReturn, Union
37813781

37823782
def foo() -> Union[Dict[str, Any], Dict[int, Any]]:
37833783
return {}
3784+
[builtins fixtures/dict.pyi]
3785+
3786+
[case testExistingEmptyCollectionDoesNotUpcast]
3787+
from typing import Any, Dict, NoReturn, NoReturn, Union
37843788

37853789
empty: Dict[NoReturn, NoReturn]
3790+
3791+
def foo() -> Dict[str, Any]:
3792+
return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "dict[str, Any]")
3793+
37863794
def bar() -> Union[Dict[str, Any], Dict[int, Any]]:
3787-
return empty
3795+
return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "Union[dict[str, Any], dict[int, Any]]")
37883796
[builtins fixtures/dict.pyi]
37893797

37903798
[case testUpperBoundInferenceFallbackNotOverused]

0 commit comments

Comments
 (0)