Skip to content

Commit 9d37087

Browse files
Merge branch 'master' into for-filter
2 parents 009550e + bec8cba commit 9d37087

50 files changed

Lines changed: 622 additions & 381 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/source/error_code_list2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Example:
3232
3333
# mypy: disallow-any-generics
3434
35-
# Error: Missing type parameters for generic type "list" [type-arg]
35+
# Error: Missing type arguments for generic type "list" [type-arg]
3636
def remove_dups(items: list) -> list:
3737
...
3838

mypy/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,8 @@ def __init__(
21802180
self.dep_hashes = {
21812181
k: v for (k, v) in zip(self.meta.dependencies, self.meta.dep_hashes)
21822182
}
2183-
self.error_lines = self.meta.error_lines
2183+
# Only copy `error_lines` if the module is not silently imported.
2184+
self.error_lines = [] if self.ignore_all else self.meta.error_lines
21842185
if temporary:
21852186
self.load_tree(temporary=True)
21862187
if not manager.use_fine_grained_cache():

mypy/checker.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8394,43 +8394,42 @@ def conditional_types(
83948394

83958395
if isinstance(proper_type, AnyType):
83968396
return proposed_type, current_type
8397-
elif isinstance(proposed_type, AnyType):
8397+
if isinstance(proposed_type, AnyType):
83988398
# We don't really know much about the proposed type, so we shouldn't
83998399
# attempt to narrow anything. Instead, we broaden the expr to Any to
84008400
# avoid false positives
84018401
return proposed_type, default
8402-
elif not any(type_range.is_upper_bound for type_range in proposed_type_ranges) and (
8403-
# concrete subtypes
8404-
is_proper_subtype(current_type, proposed_type, ignore_promotions=True)
8402+
if not any(type_range.is_upper_bound for type_range in proposed_type_ranges):
8403+
# concrete subtype
8404+
if is_proper_subtype(current_type, proposed_type, ignore_promotions=True):
8405+
return default, UninhabitedType()
8406+
84058407
# structural subtypes
8406-
or (
8407-
(
8408-
isinstance(proposed_type, CallableType)
8409-
or (isinstance(proposed_type, Instance) and proposed_type.type.is_protocol)
8408+
if (
8409+
isinstance(proposed_type, CallableType)
8410+
or (isinstance(proposed_type, Instance) and proposed_type.type.is_protocol)
8411+
) and is_subtype(current_type, proposed_type, ignore_promotions=True):
8412+
# Note: It's possible that current_type=`Any | Proto` while proposed_type=`Proto`
8413+
# so we cannot return `Never` for the else branch
8414+
remainder = restrict_subtype_away(
8415+
current_type,
8416+
default if default is not None else proposed_type,
8417+
consider_runtime_isinstance=consider_runtime_isinstance,
84108418
)
8411-
and is_subtype(current_type, proposed_type, ignore_promotions=True)
8412-
)
8413-
):
8414-
# Expression is always of one of the types in proposed_type_ranges
8415-
return default, UninhabitedType()
8416-
elif not is_overlapping_types(current_type, proposed_type, ignore_promotions=True):
8419+
return default, remainder
8420+
if not is_overlapping_types(current_type, proposed_type, ignore_promotions=True):
84178421
# Expression is never of any type in proposed_type_ranges
84188422
return UninhabitedType(), default
8419-
else:
8420-
# we can only restrict when the type is precise, not bounded
8421-
proposed_precise_type = UnionType.make_union(
8422-
[
8423-
type_range.item
8424-
for type_range in proposed_type_ranges
8425-
if not type_range.is_upper_bound
8426-
]
8427-
)
8428-
remaining_type = restrict_subtype_away(
8429-
current_type,
8430-
proposed_precise_type,
8431-
consider_runtime_isinstance=consider_runtime_isinstance,
8432-
)
8433-
return proposed_type, remaining_type
8423+
# we can only restrict when the type is precise, not bounded
8424+
proposed_precise_type = UnionType.make_union(
8425+
[type_range.item for type_range in proposed_type_ranges if not type_range.is_upper_bound]
8426+
)
8427+
remaining_type = restrict_subtype_away(
8428+
current_type,
8429+
proposed_precise_type,
8430+
consider_runtime_isinstance=consider_runtime_isinstance,
8431+
)
8432+
return proposed_type, remaining_type
84348433

84358434

84368435
def conditional_types_to_typemaps(

mypy/checkstrformat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ def __init__(
145145
self.key = m_dict.get("key")
146146

147147
# Replace unmatched optional groups with empty matches (for convenience).
148-
self.conv_type = m_dict.get("type", "")
149-
self.flags = m_dict.get("flags", "")
150-
self.width = m_dict.get("width", "")
151-
self.precision = m_dict.get("precision", "")
148+
self.conv_type = m_dict.get("type") or ""
149+
self.flags = m_dict.get("flags") or ""
150+
self.width = m_dict.get("width") or ""
151+
self.precision = m_dict.get("precision") or ""
152152

153153
# Used only for str.format() calls (it may be custom for types with __format__()).
154154
self.format_spec = m_dict.get("format_spec")

mypy/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
816816
if isinstance(actual, Overloaded) and actual.fallback is not None:
817817
actual = actual.fallback
818818
if isinstance(actual, TypedDictType):
819-
actual = actual.as_anonymous().fallback
819+
actual = actual.create_anonymous_fallback()
820820
if isinstance(actual, LiteralType):
821821
actual = actual.fallback
822822
if isinstance(actual, Instance):

mypy/meet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ def typed_dict_mapping_overlap(
12631263
key_type, value_type = get_proper_types(other.args)
12641264

12651265
# TODO: is there a cleaner way to get str_type here?
1266-
fallback = typed.as_anonymous().fallback
1266+
fallback = typed.create_anonymous_fallback()
12671267
str_type = fallback.type.bases[0].args[0] # typing._TypedDict inherits Mapping[str, object]
12681268

12691269
# Special case: a TypedDict with no required keys overlaps with an empty dict.

mypy/message_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
176176
"Access to generic instance variables via class is ambiguous"
177177
)
178178
GENERIC_CLASS_VAR_ACCESS: Final = "Access to generic class variables is ambiguous"
179-
BARE_GENERIC: Final = "Missing type parameters for generic type {}"
179+
BARE_GENERIC: Final = "Missing type arguments for generic type {}"
180180
IMPLICIT_GENERIC_ANY_BUILTIN: Final = (
181181
'Implicit generic "Any". Use "{}" and specify generic parameters'
182182
)

mypy/plugins/default.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,9 @@ def typed_dict_update_signature_callback(ctx: MethodSigContext) -> CallableType:
479479
arg_type = get_proper_type(signature.arg_types[0])
480480
if not isinstance(arg_type, TypedDictType):
481481
return signature
482-
arg_type = arg_type.as_anonymous()
483-
arg_type = arg_type.copy_modified(required_keys=set())
482+
arg_type = ctx.type.copy_modified(
483+
fallback=arg_type.create_anonymous_fallback(), required_keys=set()
484+
)
484485
if ctx.args and ctx.args[0]:
485486
if signature.name in _TP_DICT_MUTATING_METHODS:
486487
# If we want to mutate this object in place, we need to set this flag,

mypy/semanal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,6 +3224,12 @@ def visit_import_all(self, i: ImportAll) -> None:
32243224
self.add_imported_symbol(
32253225
name, node, context=i, module_public=True, module_hidden=False
32263226
)
3227+
# This is a (minimalist) copy of the logic in visit_import_from(), we need
3228+
# to clean-up any remaining placeholders by replacing them with Var(Any).
3229+
if isinstance(node.node, PlaceholderNode) and self.final_iteration:
3230+
self.add_unknown_imported_symbol(
3231+
name, i, target_name=None, module_public=True, module_hidden=False
3232+
)
32273233

32283234
else:
32293235
# Don't add any dummy symbols for 'from x import *' if 'x' is unknown.

mypy/types.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,11 +3080,11 @@ def is_final(self) -> bool:
30803080
def is_anonymous(self) -> bool:
30813081
return self.fallback.type.fullname in TPDICT_FB_NAMES
30823082

3083-
def as_anonymous(self) -> TypedDictType:
3083+
def create_anonymous_fallback(self) -> Instance:
30843084
if self.is_anonymous():
3085-
return self
3085+
return self.fallback
30863086
assert self.fallback.type.typeddict_type is not None
3087-
return self.fallback.type.typeddict_type.as_anonymous()
3087+
return self.fallback.type.typeddict_type.create_anonymous_fallback()
30883088

30893089
def copy_modified(
30903090
self,
@@ -3110,10 +3110,6 @@ def copy_modified(
31103110
required_keys &= set(item_names)
31113111
return TypedDictType(items, required_keys, readonly_keys, fallback, self.line, self.column)
31123112

3113-
def create_anonymous_fallback(self) -> Instance:
3114-
anonymous = self.as_anonymous()
3115-
return anonymous.fallback
3116-
31173113
def names_are_wider_than(self, other: TypedDictType) -> bool:
31183114
return len(other.items.keys() - self.items.keys()) == 0
31193115

0 commit comments

Comments
 (0)