Skip to content

Commit 374d4dd

Browse files
authored
Merge branch 'master' into bugfix/st-none-typevar-overlap
2 parents 207c75a + 1ec2ef3 commit 374d4dd

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

mypy/checkpattern.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ def visit_or_pattern(self, o: OrPattern) -> PatternType:
158158
for pattern in o.patterns:
159159
pattern_type = self.accept(pattern, current_type)
160160
pattern_types.append(pattern_type)
161-
current_type = pattern_type.rest_type
161+
if not is_uninhabited(pattern_type.type):
162+
current_type = pattern_type.rest_type
162163

163164
#
164165
# Collect the final type
@@ -693,7 +694,9 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
693694

694695
def should_self_match(self, typ: Type) -> bool:
695696
typ = get_proper_type(typ)
696-
if isinstance(typ, Instance) and typ.type.is_named_tuple:
697+
if isinstance(typ, Instance) and typ.type.get("__match_args__") is not None:
698+
# Named tuples and other subtypes of builtins that define __match_args__
699+
# should not self match.
697700
return False
698701
for other in self.self_match_types:
699702
if is_subtype(typ, other):

mypy/test/data.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def __init__(
304304
self.data = data
305305
self.line = line
306306
self.old_cwd: str | None = None
307-
self.tmpdir: tempfile.TemporaryDirectory[str] | None = None
307+
self.tmpdir: str | None = None
308308

309309
def runtest(self) -> None:
310310
if self.skip:
@@ -323,19 +323,19 @@ def runtest(self) -> None:
323323
save_dir: str | None = self.config.getoption("--save-failures-to", None)
324324
if save_dir:
325325
assert self.tmpdir is not None
326-
target_dir = os.path.join(save_dir, os.path.basename(self.tmpdir.name))
326+
target_dir = os.path.join(save_dir, os.path.basename(self.tmpdir))
327327
print(f"Copying data from test {self.name} to {target_dir}")
328328
if not os.path.isabs(target_dir):
329329
assert self.old_cwd
330330
target_dir = os.path.join(self.old_cwd, target_dir)
331-
shutil.copytree(self.tmpdir.name, target_dir)
331+
shutil.copytree(self.tmpdir, target_dir)
332332
raise
333333

334334
def setup(self) -> None:
335335
parse_test_case(case=self)
336336
self.old_cwd = os.getcwd()
337-
self.tmpdir = tempfile.TemporaryDirectory(prefix="mypy-test-")
338-
os.chdir(self.tmpdir.name)
337+
self.tmpdir = tempfile.mkdtemp(prefix="mypy-test-")
338+
os.chdir(self.tmpdir)
339339
os.mkdir(test_temp_dir)
340340

341341
# Precalculate steps for find_steps()
@@ -371,10 +371,7 @@ def teardown(self) -> None:
371371
if self.old_cwd is not None:
372372
os.chdir(self.old_cwd)
373373
if self.tmpdir is not None:
374-
try:
375-
self.tmpdir.cleanup()
376-
except OSError:
377-
pass
374+
shutil.rmtree(self.tmpdir, ignore_errors=True)
378375
self.old_cwd = None
379376
self.tmpdir = None
380377

mypy/test/testfinegrained.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
101101
if messages:
102102
a.extend(normalize_messages(messages))
103103

104-
assert testcase.tmpdir
105-
a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name))
104+
assert testcase.tmpdir is not None
105+
a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir))
106106
a.extend(self.maybe_inspect(step, server, main_src))
107107

108108
if server.fine_grained_manager:
@@ -248,8 +248,8 @@ def perform_step(
248248
new_messages = normalize_messages(new_messages)
249249

250250
a = new_messages
251-
assert testcase.tmpdir
252-
a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name))
251+
assert testcase.tmpdir is not None
252+
a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir))
253253
a.extend(self.maybe_inspect(step, server, main_src))
254254

255255
return a, triggered

test-data/unit/check-python310.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,25 @@ match m:
648648
reveal_type(m) # N: Revealed type is "builtins.tuple[Any, ...]"
649649
[builtins fixtures/primitives.pyi]
650650

651+
[case testMatchClassPatternCaptureSelfSubtype]
652+
class A(str):
653+
pass
654+
655+
class B(str):
656+
__match_args__ = ("b",)
657+
b: int
658+
659+
def f1(x: A):
660+
match x:
661+
case A(a):
662+
reveal_type(a) # N: Revealed type is "__main__.A"
663+
664+
def f2(x: B):
665+
match x:
666+
case B(b):
667+
reveal_type(b) # N: Revealed type is "builtins.int"
668+
[builtins fixtures/tuple.pyi]
669+
651670
[case testMatchInvalidClassPattern]
652671
m: object
653672

@@ -1702,6 +1721,22 @@ def f(x: int | str) -> int:
17021721
case str() as s:
17031722
return 1
17041723

1724+
[case testMatchOrPatternExhaustiveness]
1725+
from typing import NoReturn, Literal
1726+
def assert_never(x: NoReturn) -> None: ...
1727+
1728+
Color = Literal["blue", "green", "red"]
1729+
c: Color
1730+
1731+
match c:
1732+
case "blue":
1733+
reveal_type(c) # N: Revealed type is "Literal['blue']"
1734+
case "green" | "notColor":
1735+
reveal_type(c) # N: Revealed type is "Literal['green']"
1736+
case _:
1737+
assert_never(c) # E: Argument 1 to "assert_never" has incompatible type "Literal['red']"; expected "Never"
1738+
[typing fixtures/typing-typeddict.pyi]
1739+
17051740
[case testMatchAsPatternIntersection-skip]
17061741
class A: pass
17071742
class B: pass

0 commit comments

Comments
 (0)