Skip to content

Commit 582e455

Browse files
authored
Merge branch 'master' into newfeature
2 parents bb2b468 + 284ba55 commit 582e455

26 files changed

+232
-95
lines changed

mypy-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ typing_extensions>=4.6.0
44
mypy_extensions>=1.0.0
55
pathspec>=0.9.0
66
tomli>=1.1.0; python_version<'3.11'
7-
librt>=0.6.2
7+
librt>=0.6.2; platform_python_implementation != 'PyPy'

mypy/checkpattern.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Type,
4747
TypedDictType,
4848
TypeOfAny,
49+
TypeType,
4950
TypeVarTupleType,
5051
TypeVarType,
5152
UninhabitedType,
@@ -556,6 +557,8 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
556557
fallback = self.chk.named_type("builtins.function")
557558
any_type = AnyType(TypeOfAny.unannotated)
558559
typ = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback)
560+
elif isinstance(p_typ, TypeType) and isinstance(p_typ.item, NoneType):
561+
typ = p_typ.item
559562
elif not isinstance(p_typ, AnyType):
560563
self.msg.fail(
561564
message_registry.CLASS_PATTERN_TYPE_REQUIRED.format(

mypy/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ def add_invertible_flag(
815815
add_invertible_flag(
816816
"--force-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
817817
)
818+
# For internal use only! Will be removed once Mypy drops support for Python 3.9.
819+
add_invertible_flag(
820+
"--overwrite-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
821+
)
818822

819823
lint_group = parser.add_argument_group(
820824
title="Configuring warnings",

mypy/options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ def __init__(self) -> None:
413413
# Deprecated, Mypy only supports Python 3.9+
414414
self.force_uppercase_builtins = False
415415
self.force_union_syntax = False
416+
# Mypy internal use only! Set during test run.
417+
self.overwrite_union_syntax = False
416418

417419
# Sets custom output format
418420
self.output: str | None = None
@@ -434,7 +436,7 @@ def use_lowercase_names(self) -> bool:
434436
def use_or_syntax(self) -> bool:
435437
if self.python_version >= (3, 10):
436438
return not self.force_union_syntax
437-
return False
439+
return self.overwrite_union_syntax
438440

439441
def use_star_unpack(self) -> bool:
440442
return self.python_version >= (3, 11)

mypy/test/testcmdline.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
6060
args = parse_args(testcase.input[0])
6161
custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None
6262
args.append("--show-traceback")
63+
args.append("--overwrite-union-syntax")
6364
if "--error-summary" not in args:
6465
args.append("--no-error-summary")
6566
if "--show-error-codes" not in args:
6667
args.append("--hide-error-codes")
6768
if "--disallow-empty-bodies" not in args:
6869
args.append("--allow-empty-bodies")
69-
if "--no-force-union-syntax" not in args:
70-
args.append("--force-union-syntax")
7170
# Type check the program.
7271
fixed = [python3_path, "-m", "mypy"]
7372
env = os.environ.copy()

mypy/test/testpythoneval.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
5252
"--no-error-summary",
5353
"--hide-error-codes",
5454
"--allow-empty-bodies",
55+
"--overwrite-union-syntax",
5556
"--test-env", # Speeds up some checks
5657
]
5758
interpreter = python3_path
@@ -71,9 +72,6 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
7172
return
7273
mypy_cmdline.extend(additional_flags)
7374

74-
if "--no-force-union-syntax" not in mypy_cmdline:
75-
mypy_cmdline.append("--force-union-syntax")
76-
7775
# Write the program to a file.
7876
program = "_" + testcase.name + ".py"
7977
program_path = os.path.join(test_temp_dir, program)

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,6 +3736,7 @@ class TypeStrVisitor(SyntheticTypeVisitor[str]):
37363736
Notes:
37373737
- Represent unbound types as Foo? or Foo?[...].
37383738
- Represent the NoneType type as None.
3739+
- Represent Union[x, y] as x | y
37393740
"""
37403741

37413742
def __init__(self, id_mapper: IdMapper | None = None, *, options: Options) -> None:

mypyc/ir/deps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ def get_header(self) -> str:
5050
LIBRT_BASE64: Final = Capsule("librt.base64")
5151

5252
BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c")
53+
BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c")

mypyc/irbuild/builder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,10 @@ def get_sequence_type_from_type(self, target_type: Type) -> RType:
990990
elif isinstance(target_type, TypeVarLikeType):
991991
return self.get_sequence_type_from_type(target_type.upper_bound)
992992
elif isinstance(target_type, TupleType):
993+
items = target_type.items
994+
assert items, "This function does not support empty tuples"
993995
# Tuple might have elements of different types.
994-
rtypes = {self.mapper.type_to_rtype(item) for item in target_type.items}
996+
rtypes = set(map(self.mapper.type_to_rtype, items))
995997
if len(rtypes) == 1:
996998
return rtypes.pop()
997999
else:

mypyc/irbuild/for_helpers.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from __future__ import annotations
99

1010
from collections.abc import Callable
11-
from typing import ClassVar
11+
from typing import ClassVar, cast
1212

1313
from mypy.nodes import (
1414
ARG_POS,
@@ -242,25 +242,45 @@ def sequence_from_generator_preallocate_helper(
242242
rtype = builder.node_type(sequence_expr)
243243
if not (is_sequence_rprimitive(rtype) or isinstance(rtype, RTuple)):
244244
return None
245-
sequence = builder.accept(sequence_expr)
246-
length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True)
245+
247246
if isinstance(rtype, RTuple):
248247
# If input is RTuple, box it to tuple_rprimitive for generic iteration
249248
# TODO: this can be optimized a bit better with an unrolled ForRTuple helper
250249
proper_type = get_proper_type(builder.types[sequence_expr])
251250
assert isinstance(proper_type, TupleType), proper_type
252251

253-
get_item_ops = [
254-
(
255-
LoadLiteral(typ.value, object_rprimitive)
256-
if isinstance(typ, LiteralType)
257-
else TupleGet(sequence, i, line)
258-
)
259-
for i, typ in enumerate(get_proper_types(proper_type.items))
260-
]
252+
# the for_loop_helper_with_index crashes for empty tuples, bail out
253+
if not proper_type.items:
254+
return None
255+
256+
proper_types = get_proper_types(proper_type.items)
257+
258+
get_item_ops: list[LoadLiteral | TupleGet]
259+
if all(isinstance(typ, LiteralType) for typ in proper_types):
260+
get_item_ops = [
261+
LoadLiteral(cast(LiteralType, typ).value, object_rprimitive)
262+
for typ in proper_types
263+
]
264+
265+
else:
266+
sequence = builder.accept(sequence_expr)
267+
get_item_ops = [
268+
(
269+
LoadLiteral(typ.value, object_rprimitive)
270+
if isinstance(typ, LiteralType)
271+
else TupleGet(sequence, i, line)
272+
)
273+
for i, typ in enumerate(proper_types)
274+
]
275+
261276
items = list(map(builder.add, get_item_ops))
262277
sequence = builder.new_tuple(items, line)
263278

279+
else:
280+
sequence = builder.accept(sequence_expr)
281+
282+
length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True)
283+
264284
target_op = empty_op_llbuilder(length, line)
265285

266286
def set_item(item_index: Value) -> None:

0 commit comments

Comments
 (0)