Skip to content

Commit 5045864

Browse files
Merge branch 'master' into patch-2
2 parents f0ed64b + 8ed16d1 commit 5045864

File tree

14 files changed

+99
-34
lines changed

14 files changed

+99
-34
lines changed

mypy/checker.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,10 @@ def reset(self) -> None:
437437
self._type_maps[0].clear()
438438
self.temp_type_map = None
439439
self.expr_checker.reset()
440-
441-
assert self.inferred_attribute_types is None
442-
assert self.partial_types == []
443-
assert self.deferred_nodes == []
444-
assert len(self.scope.stack) == 1
445-
assert self.partial_types == []
440+
self.deferred_nodes = []
441+
self.partial_types = []
442+
self.inferred_attribute_types = None
443+
self.scope = CheckerScope(self.tree)
446444

447445
def check_first_pass(self) -> None:
448446
"""Type check the entire file, but defer functions with unresolved references.
@@ -1557,6 +1555,10 @@ def is_var_redefined_in_outer_context(self, v: Var, after_line: int) -> bool:
15571555
Note that this doesn't do a full CFG analysis but uses a line number based
15581556
heuristic that isn't correct in some (rare) cases.
15591557
"""
1558+
if v.is_final:
1559+
# Final vars are definitely never reassigned.
1560+
return False
1561+
15601562
outers = self.tscope.outer_functions()
15611563
if not outers:
15621564
# Top-level function -- outer context is top level, and we can't reason about

mypy/checkexpr.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,12 @@ def infer_function_type_arguments_using_context(
20132013
# variables in an expression are inferred at the same time.
20142014
# (And this is hard, also we need to be careful with lambdas that require
20152015
# two passes.)
2016-
if isinstance(ret_type, TypeVarType):
2016+
proper_ret = get_proper_type(ret_type)
2017+
if (
2018+
isinstance(proper_ret, TypeVarType)
2019+
or isinstance(proper_ret, UnionType)
2020+
and all(isinstance(get_proper_type(u), TypeVarType) for u in proper_ret.items)
2021+
):
20172022
# Another special case: the return type is a type variable. If it's unrestricted,
20182023
# we could infer a too general type for the type variable if we use context,
20192024
# and this could result in confusing and spurious type errors elsewhere.

mypy/dmypy/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ def check_output(
562562
563563
Call sys.exit() unless the status code is zero.
564564
"""
565+
if os.name == "nt":
566+
# Enable ANSI color codes for Windows cmd using this strange workaround
567+
# ( see https://github.com/python/cpython/issues/74261 )
568+
os.system("")
565569
if "error" in response:
566570
fail(response["error"])
567571
try:

mypy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def add_invertible_flag(
917917
add_invertible_flag(
918918
"--strict-bytes",
919919
default=False,
920-
strict_flag=False,
920+
strict_flag=True,
921921
help="Disable treating bytearray and memoryview as subtypes of bytes",
922922
group=strictness_group,
923923
)

mypy/stubgen.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]:
565565
default = "..."
566566
if arg_.initializer:
567567
if not typename:
568-
typename = self.get_str_type_of_node(arg_.initializer, True, False)
568+
typename = self.get_str_type_of_node(arg_.initializer, can_be_incomplete=False)
569569
potential_default, valid = self.get_str_default_of_node(arg_.initializer)
570570
if valid and len(potential_default) <= 200:
571571
default = potential_default
@@ -1305,9 +1305,7 @@ def is_private_member(self, fullname: str) -> bool:
13051305
parts = fullname.split(".")
13061306
return any(self.is_private_name(part) for part in parts)
13071307

1308-
def get_str_type_of_node(
1309-
self, rvalue: Expression, can_infer_optional: bool = False, can_be_any: bool = True
1310-
) -> str:
1308+
def get_str_type_of_node(self, rvalue: Expression, *, can_be_incomplete: bool = True) -> str:
13111309
rvalue = self.maybe_unwrap_unary_expr(rvalue)
13121310

13131311
if isinstance(rvalue, IntExpr):
@@ -1327,9 +1325,7 @@ def get_str_type_of_node(
13271325
return "complex"
13281326
if isinstance(rvalue, NameExpr) and rvalue.name in ("True", "False"):
13291327
return "bool"
1330-
if can_infer_optional and isinstance(rvalue, NameExpr) and rvalue.name == "None":
1331-
return f"{self.add_name('_typeshed.Incomplete')} | None"
1332-
if can_be_any:
1328+
if can_be_incomplete:
13331329
return self.add_name("_typeshed.Incomplete")
13341330
else:
13351331
return ""

mypy/stubgenc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,11 @@ def get_type_fullname(self, typ: type) -> str:
769769
return "Any"
770770
typename = getattr(typ, "__qualname__", typ.__name__)
771771
module_name = self.get_obj_module(typ)
772-
assert module_name is not None, typ
772+
if module_name is None:
773+
# This should not normally happen, but some types may resist our
774+
# introspection attempts too hard. See
775+
# https://github.com/python/mypy/issues/19031
776+
return "_typeshed.Incomplete"
773777
if module_name != "builtins":
774778
typename = f"{module_name}.{typename}"
775779
return typename

mypy/stubtest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ def verify_typealias(
14941494
"__loader__",
14951495
"__spec__",
14961496
"__annotations__",
1497+
"__annotate__",
14971498
"__path__", # mypy adds __path__ to packages, but C packages don't have it
14981499
"__getattr__", # resulting behaviour might be typed explicitly
14991500
# Created by `warnings.warn`, does not make much sense to have in stubs:
@@ -1510,6 +1511,9 @@ def verify_typealias(
15101511
# Special attributes
15111512
"__dict__",
15121513
"__annotations__",
1514+
"__annotate__",
1515+
"__annotations_cache__",
1516+
"__annotate_func__",
15131517
"__text_signature__",
15141518
"__weakref__",
15151519
"__hash__",
@@ -1518,6 +1522,7 @@ def verify_typealias(
15181522
"__vectorcalloffset__", # undocumented implementation detail of the vectorcall protocol
15191523
"__firstlineno__",
15201524
"__static_attributes__",
1525+
"__classdictcell__",
15211526
# isinstance/issubclass hooks that type-checkers don't usually care about
15221527
"__instancecheck__",
15231528
"__subclasshook__",
@@ -1536,6 +1541,7 @@ def verify_typealias(
15361541
"__getinitargs__",
15371542
"__reduce_ex__",
15381543
"__reduce__",
1544+
"__slotnames__", # Cached names of slots added by `copyreg` module.
15391545
# ctypes weirdness
15401546
"__ctype_be__",
15411547
"__ctype_le__",

mypy/typeshed/stdlib/tempfile.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class SpooledTemporaryFile(IO[AnyStr], _SpooledTemporaryFileBase):
387387
def write(self: SpooledTemporaryFile[bytes], s: ReadableBuffer) -> int: ...
388388
@overload
389389
def write(self, s: AnyStr) -> int: ...
390-
@overload
390+
@overload # type: ignore[override]
391391
def writelines(self: SpooledTemporaryFile[str], iterable: Iterable[str]) -> None: ...
392392
@overload
393393
def writelines(self: SpooledTemporaryFile[bytes], iterable: Iterable[ReadableBuffer]) -> None: ...

mypy/typeshed/stdlib/tkinter/ttk.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ class Notebook(Widget):
562562
compound: tkinter._Compound = ...,
563563
underline: int = ...,
564564
) -> None: ...
565-
def forget(self, tab_id) -> None: ...
565+
def forget(self, tab_id) -> None: ... # type: ignore[override]
566566
def hide(self, tab_id) -> None: ...
567567
def identify(self, x: int, y: int) -> str: ...
568568
def index(self, tab_id): ...

mypy_self_check.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[mypy]
22

33
strict = True
4-
strict_bytes = True
54
local_partial_types = True
65
disallow_any_unimported = True
76
show_traceback = True

0 commit comments

Comments
 (0)