Skip to content

Commit c2af54f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into bugfix/st-none-typevar-overlap
2 parents 374d4dd + 880eb87 commit c2af54f

29 files changed

+299
-111
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
exclude: '^(mypyc/external/)|(mypy/typeshed/)|misc/typeshed_patches' # Exclude all vendored code from lints
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.5.0 # must match test-requirements.txt
4+
rev: v4.5.0
55
hooks:
66
- id: trailing-whitespace
77
- id: end-of-file-fixer
88
- repo: https://github.com/psf/black-pre-commit-mirror
9-
rev: 24.8.0 # must match test-requirements.txt
9+
rev: 24.8.0
1010
hooks:
1111
- id: black
1212
exclude: '^(test-data/)'
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.6.9 # must match test-requirements.txt
14+
rev: v0.6.9
1515
hooks:
1616
- id: ruff
1717
args: [--exit-non-zero-on-fix]

mypy/checkpattern.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
TypedDictType,
4747
TypeOfAny,
4848
TypeVarTupleType,
49+
TypeVarType,
4950
UninhabitedType,
5051
UnionType,
5152
UnpackType,
@@ -343,13 +344,11 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
343344
new_inner_type = UninhabitedType()
344345
for typ in new_inner_types:
345346
new_inner_type = join_types(new_inner_type, typ)
346-
new_type = self.construct_sequence_child(current_type, new_inner_type)
347-
if is_subtype(new_type, current_type):
348-
new_type, _ = self.chk.conditional_types_with_intersection(
349-
current_type, [get_type_range(new_type)], o, default=current_type
350-
)
347+
if isinstance(current_type, TypeVarType):
348+
new_bound = self.narrow_sequence_child(current_type.upper_bound, new_inner_type, o)
349+
new_type = current_type.copy_modified(upper_bound=new_bound)
351350
else:
352-
new_type = current_type
351+
new_type = self.narrow_sequence_child(current_type, new_inner_type, o)
353352
return PatternType(new_type, rest_type, captures)
354353

355354
def get_sequence_type(self, t: Type, context: Context) -> Type | None:
@@ -448,6 +447,16 @@ def expand_starred_pattern_types(
448447

449448
return new_types
450449

450+
def narrow_sequence_child(self, outer_type: Type, inner_type: Type, ctx: Context) -> Type:
451+
new_type = self.construct_sequence_child(outer_type, inner_type)
452+
if is_subtype(new_type, outer_type):
453+
new_type, _ = self.chk.conditional_types_with_intersection(
454+
outer_type, [get_type_range(new_type)], ctx, default=outer_type
455+
)
456+
else:
457+
new_type = outer_type
458+
return new_type
459+
451460
def visit_starred_pattern(self, o: StarredPattern) -> PatternType:
452461
captures: dict[Expression, Type] = {}
453462
if o.capture is not None:

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
17271727
return visitor.visit_str_expr(self)
17281728

17291729

1730-
def is_StrExpr_list(seq: list[Expression]) -> TypeGuard[list[StrExpr]]:
1730+
def is_StrExpr_list(seq: list[Expression]) -> TypeGuard[list[StrExpr]]: # noqa: N802
17311731
return all(isinstance(item, StrExpr) for item in seq)
17321732

17331733

mypy/stubutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,8 @@ def is_not_in_all(self, name: str) -> bool:
832832
return False
833833

834834
def is_private_name(self, name: str, fullname: str | None = None) -> bool:
835+
if "__mypy-" in name:
836+
return True # Never include mypy generated symbols
835837
if self._include_private:
836838
return False
837839
if fullname in self.EXTRA_EXPORTED:

mypy/test/teststubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ def test(cls, arg0: str) -> None:
987987
def test_generate_c_type_classmethod_with_overloads(self) -> None:
988988
class TestClass:
989989
@classmethod
990-
def test(self, arg0: str) -> None:
990+
def test(cls, arg0: str) -> None:
991991
"""
992992
test(cls, arg0: str)
993993
test(cls, arg0: int)

mypy/typeops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,16 @@ def _remove_redundant_union_items(items: list[Type], keep_erased: bool) -> list[
650650
def _get_type_method_ret_type(t: Type, *, name: str) -> Type | None:
651651
t = get_proper_type(t)
652652

653+
# For Enum literals the ret_type can change based on the Enum
654+
# we need to check the type of the enum rather than the literal
655+
if isinstance(t, LiteralType) and t.is_enum_literal():
656+
t = t.fallback
657+
653658
if isinstance(t, Instance):
654659
sym = t.type.get(name)
660+
# Fallback to the metaclass for the lookup when necessary
661+
if not sym and (m := t.type.metaclass_type):
662+
sym = m.type.get(name)
655663
if sym:
656664
sym_type = get_proper_type(sym.type)
657665
if isinstance(sym_type, CallableType):

mypy/types.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,10 +2818,28 @@ def __init__(
28182818
self.fallback = fallback
28192819
self._hash = -1 # Cached hash value
28202820

2821+
# NOTE: Enum types are always truthy by default, but this can be changed
2822+
# in subclasses, so we need to get the truthyness from the Enum
2823+
# type rather than base it on the value (which is a non-empty
2824+
# string for enums, so always truthy)
2825+
# TODO: We should consider moving this branch to the `can_be_true`
2826+
# `can_be_false` properties instead, so the truthyness only
2827+
# needs to be determined once per set of Enum literals.
2828+
# However, the same can be said for `TypeAliasType` in some
2829+
# cases and we only set the default based on the type it is
2830+
# aliasing. So if we decide to change this, we may want to
2831+
# change that as well. perf_compare output was inconclusive
2832+
# but slightly favored this version, probably because we have
2833+
# almost no test cases where we would redundantly compute
2834+
# `can_be_false`/`can_be_true`.
28212835
def can_be_false_default(self) -> bool:
2836+
if self.fallback.type.is_enum:
2837+
return self.fallback.can_be_false
28222838
return not self.value
28232839

28242840
def can_be_true_default(self) -> bool:
2841+
if self.fallback.type.is_enum:
2842+
return self.fallback.can_be_true
28252843
return bool(self.value)
28262844

28272845
def accept(self, visitor: TypeVisitor[T]) -> T:

mypy/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,17 @@ def quote_docstring(docstr: str) -> str:
928928
def json_dumps(obj: object, debug: bool = False) -> bytes:
929929
if orjson is not None:
930930
if debug:
931-
return orjson.dumps(obj, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
931+
dumps_option = orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
932932
else:
933933
# TODO: If we don't sort keys here, testIncrementalInternalScramble fails
934934
# We should document exactly what is going on there
935-
return orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
935+
dumps_option = orjson.OPT_SORT_KEYS
936+
937+
try:
938+
return orjson.dumps(obj, option=dumps_option) # type: ignore[no-any-return]
939+
except TypeError as e:
940+
if str(e) != "Integer exceeds 64-bit range":
941+
raise
936942

937943
if debug:
938944
return json.dumps(obj, indent=2, sort_keys=True).encode("utf-8")

mypyc/lib-rt/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
compile_args = ["--std=c++11"]
2222

2323

24-
class build_ext_custom(build_ext):
24+
class build_ext_custom(build_ext): # noqa: N801
2525
def get_library_names(self):
2626
return ["gtest"]
2727

mypyc/test/test_emitfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_integer(self) -> None:
134134
def test_tuple_get(self) -> None:
135135
self.assert_emit(TupleGet(self.t, 1, 0), "cpy_r_r0 = cpy_r_t.f1;")
136136

137-
def test_load_None(self) -> None:
137+
def test_load_None(self) -> None: # noqa: N802
138138
self.assert_emit(
139139
LoadAddress(none_object_op.type, none_object_op.src, 0),
140140
"cpy_r_r0 = (PyObject *)&_Py_NoneStruct;",

0 commit comments

Comments
 (0)