10000 Merge remote-tracking branch 'origin/master' into sync · python/mypy@e3fd8bf · GitHub
[go: up one dir, main page]

Skip to content

Commit e3fd8bf

Browse files
author
hauntsaninja
committed
Merge remote-tracking branch 'origin/master' into sync
2 parents 63b0532 + 48d810d commit e3fd8bf

Some content is hidden

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

89 files changed

+719
-16440
lines changed

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ DEALINGS IN THE SOFTWARE.
2626

2727
= = = = =
2828

29-
Portions of mypy and mypyc are licensed under different licenses. The
30-
files under stdlib-samples as well as the files
29+
Portions of mypy and mypyc are licensed under different licenses.
30+
The files
3131
mypyc/lib-rt/pythonsupport.h, mypyc/lib-rt/getargs.c and
3232
mypyc/lib-rt/getargsfast.c are licensed under the PSF 2 License, reproduced
3333
below.

mypy/checker.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ def check_compatibility_final_super(self, node: Var,
25172517
self.msg.cant_override_final(node.name, base.name, node)
25182518
return False
25192519
if node.is_final:
2520-
if base.fullname in ENUM_BASES and node.name in ENUM_SPECIAL_PROPS:
2520+
if base.fullname in ENUM_BASES or node.name in ENUM_SPECIAL_PROPS:
25212521
return True
25222522
self.check_if_final_var_override_writable(node.name, base_node, node)
25232523
return True
@@ -4550,17 +4550,16 @@ def has_no_custom_eq_checks(t: Type) -> bool:
45504550
self._check_for_truthy_type(original_vartype, node)
45514551
vartype = try_expanding_sum_type_to_union(original_vartype, "builtins.bool")
45524552

4553-
if_type = true_only(vartype) # type: Type
4554-
else_type = false_only(vartype) # type: Type
4555-
ref = node # type: Expression
4553+
if_type = true_only(vartype)
4554+
else_type = false_only(vartype)
45564555
if_map = (
4557-
{ref: if_type}
4558-
if not isinstance(get_proper_type(if_type), UninhabitedType)
4556+
{node: if_type}
4557+
if not isinstance(if_type, UninhabitedType)
45594558
else None
45604559
)
45614560
else_map = (
4562-
{ref: else_type}
4563-
if not isinstance(get_proper_type(else_type), UninhabitedType)
4561+
{node: else_type}
4562+
if not isinstance(else_type, UninhabitedType)
45644563
else None
45654564
)
45664565
return if_map, else_map

mypy/checkexpr.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, LiteralType, LiteralValue,
2121
is_named_instance, FunctionLike, ParamSpecType, ParamSpecFlavor,
2222
StarType, is_optional, remove_optional, is_generic_instance, get_proper_type, ProperType,
23-
get_proper_types, flatten_nested_unions
23+
get_proper_types, flatten_nested_unions, LITERAL_TYPE_NAMES,
2424
)
2525
from mypy.nodes import (
2626
NameExpr, RefExpr, Var, FuncDef, OverloadedFuncDef, TypeInfo, CallExpr,
@@ -2064,11 +2064,19 @@ def check_union_call(self,
20642064
arg_names: Optional[Sequence[Optional[str]]],
20652065
context: Context,
20662066
arg_messages: MessageBuilder) -> Tuple[Type, Type]:
2067-
self.msg.disable_type_names += 1
2068-
results = [self.check_call(subtype, args, arg_kinds, context, arg_names,
2069-
arg_messages=arg_messages)
2070-
for subtype in callee.relevant_items()]
2071-
self.msg.disable_type_names -= 1
2067+
with self.msg.disable_type_names():
2068+
results = [
2069+
self.check_call(
2070+
subtype,
2071+
args,
2072+
arg_kinds,
2073+
context,
2074+
arg_names,
2075+
arg_messages=arg_messages,
2076+
)
2077+
for subtype in callee.relevant_items()
2078+
]
2079+
20722080
return (make_simplified_union([res[0] for res in results]),
20732081
callee)
20742082

@@ -2462,11 +2470,11 @@ def check_union_method_call_by_name(self,
24622470
for typ in base_type.relevant_items():
24632471
# Format error messages consistently with
24642472
# mypy.checkmember.analyze_union_member_access().
2465-
local_errors.disable_type_names += 1
2466-
item, meth_item = self.check_method_call_by_name(method, typ, args, arg_kinds,
2467-
context, local_errors,
2468-
original_type)
2469-
local_errors.disable_type_names -= 1
2473+
with local_errors.disable_type_names():
2474+
item, meth_item = self.check_method_call_by_name(
2475+
method, typ, args, arg_kinds,
2476+
context, local_errors, original_type,
2477+
)
24702478
res.append(item)
24712479
meth_res.append(meth_item)
24722480
return make_simplified_union(res), make_simplified_union(meth_res)
@@ -4543,10 +4551,9 @@ def try_getting_literal(typ: Type) -> ProperType:
45434551

45444552
def is_expr_literal_type(node: Expression) -> bool:
45454553
"""Returns 'true' if the given node is a Literal"""
4546-
valid = ('typing.Literal', 'typing_extensions.Literal')
45474554
if isinstance(node, IndexExpr):
45484555
base = node.base
4549-
return isinstance(base, RefExpr) and base.fullname in valid
4556+
return isinstance(base, RefExpr) and base.fullname in LITERAL_TYPE_NAMES
45504557
if isinstance(node, NameExpr):
45514558
underlying = node.node
45524559
return isinstance(underlying, TypeAlias) and isinstance(get_proper_type(underlying.target),

mypy/checkmember.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,12 @@ def analyze_type_type_member_access(name: str,
311311

312312

313313
def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) -> Type:
314-
mx.msg.disable_type_names += 1
315-
results = []
316-
for subtype in typ.relevant_items():
317-
# Self types should be bound to every individual item of a union.
318-
item_mx = mx.copy_modified(self_type=subtype)
319-
results.append(_analyze_member_access(name, subtype, item_mx))
320-
mx.msg.disable_type_names -= 1
314+
with mx.msg.disable_type_names():
315+
results = []
316+
for subtype in typ.relevant_items():
317+
# Self types should be bound to every individual item of a union.
318+
item_mx = mx.copy_modified(self_type=subtype)
319+
results.append(_analyze_member_access(name, subtype, item_mx))
321320
return make_simplified_union(results)
322321

323322

mypy/constraints.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
TupleType, TypedDictType, UnionType, Overloaded, ErasedType, PartialType, DeletedType,
99
UninhabitedType, TypeType, TypeVarId, TypeQuery, is_named_instance, TypeOfAny, LiteralType,
1010
ProperType, ParamSpecType, get_proper_type, TypeAliasType, is_union_with_any,
11-
callable_with_ellipsis
11+
callable_with_ellipsis,
12+
TUPLE_LIKE_INSTANCE_NAMES,
1213
)
1314
from mypy.maptype import map_instance_to_supertype
1415
import mypy.subtypes
@@ -501,11 +502,8 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
501502
return res
502503
if isinstance(actual, AnyType):
503504
return self.infer_against_any(template.args, actual)
504-
if (isinstance(actual, TupleType) and
505-
(is_named_instance(template, 'typing.Iterable') or
506-
is_named_instance(template, 'typing.Container') or
507-
is_named_instance(template, 'typing.Sequence') or
508-
is_named_instance(template, 'typing.Reversible'))
505+
if (isinstance(actual, TupleType)
506+
and is_named_instance(template, TUPLE_LIKE_INSTANCE_NAMES)
509507
and self.direction == SUPERTYPE_OF):
510508
for item in actual.items:
511509
cb = infer_constraints(template.args[0], item, SUPERTYPE_OF)

mypy/erasetype.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def visit_uninhabited_type(self, t: UninhabitedType) -> ProperType:
4141
return t
4242

4343
def visit_erased_type(self, t: ErasedType) -> ProperType:
44-
# Should not get here.
45-
raise RuntimeError()
44+
return t
4645

4746
def visit_partial_type(self, t: PartialType) < 341A span class=pl-c1>-> ProperType:
4847
# Should not get here.

mypy/exprtotype.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mypy.fastparse import parse_type_string
1111
from mypy.types import (
1212
Type, UnboundType, TypeList, EllipsisType, AnyType, CallableArgument, TypeOfAny,
13-
RawExpressionType, ProperType, UnionType
13+
RawExpressionType, ProperType, UnionType, ANNOTATED_TYPE_NAMES,
1414
)
1515
from mypy.options import Options
1616

@@ -69,9 +69,7 @@ def expr_to_unanalyzed_type(expr: Expression,
6969
else:
7070
args = [expr.index]
7171

72-
if isinstance(expr.base, RefExpr) and expr.base.fullname in [
73-
'typing.Annotated', 'typing_extensions.Annotated'
74-
]:
72+
if isinstance(expr.base, RefExpr) and expr.base.fullname in ANNOTATED_TYPE_NAMES:
7573
# TODO: this is not the optimal solution as we are basically getting rid
7674
# of the Annotation definition and only returning the type information,
7775
# losing all the annotations.

mypy/fastparse.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from mypy.errors import Errors
4141
from mypy.options import Options
4242
from mypy.reachability import mark_block_unreachable
43+
from mypy.util import bytes_to_human_readable_repr
4344

4445
try:
4546
# pull this into a final variable to make mypyc be quiet about the
@@ -1638,17 +1639,3 @@ def stringify_name(n: AST) -> Optional[str]:
16381639
if sv is not None:
16391640
return "{}.{}".format(sv, n.attr)
16401641
return None # Can't do it.
1641-
1642-
1643-
def bytes_to_human_readable_repr(b: bytes) -> str:
1644-
"""Converts bytes into some human-readable representation. Unprintable
1645-
bytes such as the nul byte are escaped. For example:
1646-
1647-
>>> b = bytes([102, 111, 111, 10, 0])
1648-
>>> s = bytes_to_human_readable_repr(b)
1649-
>>> print(s)
1650-
foo\n\x00
1651-
>>> print(repr(s))
1652-
'foo\\n\\x00'
1653-
"""
1654-
return repr(b)[2:-1]

mypy/fastparse2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@
4747
from mypy import message_registry, errorcodes as codes
4848
from mypy.errors import Errors
4949
from mypy.fastparse import (
50-
TypeConverter, parse_type_comment, bytes_to_human_readable_repr, parse_type_ignore_tag,
50+
TypeConverter, parse_type_comment, parse_type_ignore_tag,
5151
TYPE_IGNORE_PATTERN, INVALID_TYPE_IGNORE
5252
)
5353
from mypy.options import Options
54+
from mypy.util import bytes_to_human_readable_repr
5455
from mypy.reachability import mark_block_unreachable
5556

5657
try:

mypy/messages.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ class MessageBuilder:
107107
disable_count = 0
108108

109109
# Hack to deduplicate error messages from union types
110-
disable_type_names = 0
110+
disable_type_names_count = 0
111111

112112
def __init__(self, errors: Errors, modules: Dict[str, MypyFile]) -> None:
113113
self.errors = errors
114114
self.modules = modules
115115
self.disable_count = 0
116-
self.disable_type_names = 0
116+
self.disable_type_names_count = 0
117117

118118
#
119119
# Helpers
@@ -122,7 +122,7 @@ def __init__(self, errors: Errors, modules: Dict[str, MypyFile]) -> None:
122122
def copy(self) -> 'MessageBuilder':
123123
new = MessageBuilder(self.errors.copy(), self.modules)
124124
new.disable_count = self.disable_count
125-
new.disable_type_names = self.disable_type_names
125+
new.disable_type_names_count = self.disable_type_names_count
126126
return new
127127

128128
def clean_copy(self) -> 'MessageBuilder':
@@ -145,6 +145,14 @@ def disable_errors(self) -> Iterator[None]:
145145
finally:
146146
self.disable_count -= 1
147147

148+
@contextmanager
149+
def disable_type_names(self) -> Iterator[None]:
150+
self.disable_type_names_count += 1
151+
try:
152+
yield
153+
finally:
154+
self.disable_type_names_count -= 1
155+
148156
def is_errors(self) -> bool:
149157
return self.errors.is_errors()
150158

@@ -298,7 +306,7 @@ def has_no_attr(self,
298306
extra = ' (not iterable)'
299307
elif member == '__aiter__':
300308
extra = ' (not async iterable)'
301-
if not self.disable_type_names:
309+
if not self.disable_type_names_count:
302310
failed = False
303311
if isinstance(original_type, Instance) and original_type.type.names:
304312
alternatives = set(original_type.type.names.keys())
@@ -380,7 +388,7 @@ def unsupported_operand_types(self,
380388
else:
381389
right_str = format_type(right_type)
382390

383-
if self.disable_type_names:
391+
if self.disable_type_names_count:
384392
msg = 'Unsupported operand types for {} (likely involving Union)'.format(op)
385393
else:
386394
msg = 'Unsupported operand types for {} ({} and {})'.format(
@@ -389,7 +397,7 @@ def unsupported_operand_types(self,
389397

390398
def unsupported_left_operand(self, op: str, typ: Type,
391399
context: Context) -> None:
392-
if self.disable_type_names:
400+
if self.disable_type_names_count:
393401
msg = 'Unsupported left operand type for {} (some union)'.format(op)
394402
else:
395403
msg = 'Unsupported left operand type for {} ({})'.format(

0 commit comments

Comments
 (0)
0