Skip to content

Commit

Permalink
Merge branch 'master' into pythongh-16649-type-comments-and-constrain…
Browse files Browse the repository at this point in the history
…ed-generics
  • Loading branch information
picnixz committed Feb 3, 2024
2 parents 472fcf1 + 8c2ef9d commit 9d6f56f
Show file tree
Hide file tree
Showing 323 changed files with 3,149 additions and 5,005 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1 # must match test-requirements.txt
rev: 24.1.1 # must match test-requirements.txt
hooks:
- id: black
exclude: '^(test-data/)'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.4 # must match test-requirements.txt
rev: v0.1.15 # must match test-requirements.txt
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
Expand Down
8 changes: 4 additions & 4 deletions misc/sync-typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ def main() -> None:
print("Created typeshed sync commit.")

commits_to_cherry_pick = [
"588623ff2", # LiteralString reverts
"bdcc90e85", # sum reverts
"3e5d81337", # ctypes reverts
"344298e3a", # ParamSpec for functools.wraps
"d25e4a9eb", # LiteralString reverts
"d132999ba", # sum reverts
"dd12a2d81", # ctypes reverts
"0dd4b6f75", # ParamSpec for functools.wraps
]
for commit in commits_to_cherry_pick:
try:
Expand Down
1 change: 1 addition & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
The function build() is the main interface to this module.
"""

# TODO: More consistent terminology, e.g. path/fnam, module/id, state/file

from __future__ import annotations
Expand Down
42 changes: 21 additions & 21 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,16 @@ def check_second_pass(
# print("XXX in pass %d, class %s, function %s" %
# (self.pass_num, type_name, node.fullname or node.name))
done.add(node)
with self.tscope.class_scope(
active_typeinfo
) if active_typeinfo else nullcontext():
with self.scope.push_class(
active_typeinfo
) if active_typeinfo else nullcontext():
with (
self.tscope.class_scope(active_typeinfo)
if active_typeinfo
else nullcontext()
):
with (
self.scope.push_class(active_typeinfo)
if active_typeinfo
else nullcontext()
):
self.check_partial(node)
return True

Expand Down Expand Up @@ -3802,9 +3806,11 @@ def check_multi_assignment_from_tuple(
if star_lv:
list_expr = ListExpr(
[
self.temp_node(rv_type, context)
if not isinstance(rv_type, UnpackType)
else StarExpr(self.temp_node(rv_type.type, context))
(
self.temp_node(rv_type, context)
if not isinstance(rv_type, UnpackType)
else StarExpr(self.temp_node(rv_type.type, context))
)
for rv_type in star_rv_types
]
)
Expand Down Expand Up @@ -6593,8 +6599,7 @@ def check_subtype(
notes: list[str] | None = None,
code: ErrorCode | None = None,
outer_context: Context | None = None,
) -> bool:
...
) -> bool: ...

@overload
def check_subtype(
Expand All @@ -6608,8 +6613,7 @@ def check_subtype(
*,
notes: list[str] | None = None,
outer_context: Context | None = None,
) -> bool:
...
) -> bool: ...

def check_subtype(
self,
Expand Down Expand Up @@ -7083,14 +7087,12 @@ def conditional_types_with_intersection(
type_ranges: list[TypeRange] | None,
ctx: Context,
default: None = None,
) -> tuple[Type | None, Type | None]:
...
) -> tuple[Type | None, Type | None]: ...

@overload
def conditional_types_with_intersection(
self, expr_type: Type, type_ranges: list[TypeRange] | None, ctx: Context, default: Type
) -> tuple[Type, Type]:
...
) -> tuple[Type, Type]: ...

def conditional_types_with_intersection(
self,
Expand Down Expand Up @@ -7348,15 +7350,13 @@ def visit_type_var(self, t: TypeVarType) -> None:
@overload
def conditional_types(
current_type: Type, proposed_type_ranges: list[TypeRange] | None, default: None = None
) -> tuple[Type | None, Type | None]:
...
) -> tuple[Type | None, Type | None]: ...


@overload
def conditional_types(
current_type: Type, proposed_type_ranges: list[TypeRange] | None, default: Type
) -> tuple[Type, Type]:
...
) -> tuple[Type, Type]: ...


def conditional_types(
Expand Down
16 changes: 8 additions & 8 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,11 +2132,13 @@ def infer_function_type_arguments(
unknown = UninhabitedType()
unknown.ambiguous = True
inferred_args = [
expand_type(
a, {v.id: unknown for v in list(callee_type.variables) + free_vars}
(
expand_type(
a, {v.id: unknown for v in list(callee_type.variables) + free_vars}
)
if a is not None
else None
)
if a is not None
else None
for a in poly_inferred_args
]
else:
Expand Down Expand Up @@ -6042,14 +6044,12 @@ def bool_type(self) -> Instance:
return self.named_type("builtins.bool")

@overload
def narrow_type_from_binder(self, expr: Expression, known_type: Type) -> Type:
...
def narrow_type_from_binder(self, expr: Expression, known_type: Type) -> Type: ...

@overload
def narrow_type_from_binder(
self, expr: Expression, known_type: Type, skip_non_overlapping: bool
) -> Type | None:
...
) -> Type | None: ...

def narrow_type_from_binder(
self, expr: Expression, known_type: Type, skip_non_overlapping: bool = False
Expand Down
9 changes: 4 additions & 5 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,10 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
narrowed_inner_types = []
inner_rest_types = []
for inner_type, new_inner_type in zip(inner_types, new_inner_types):
(
narrowed_inner_type,
inner_rest_type,
) = self.chk.conditional_types_with_intersection(
new_inner_type, [get_type_range(inner_type)], o, default=new_inner_type
(narrowed_inner_type, inner_rest_type) = (
self.chk.conditional_types_with_intersection(
new_inner_type, [get_type_range(inner_type)], o, default=new_inner_type
)
)
narrowed_inner_types.append(narrowed_inner_type)
inner_rest_types.append(inner_rest_type)
Expand Down
1 change: 1 addition & 0 deletions mypy/evalexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
put it in a mypyc-compiled file.
"""

import ast
from typing import Final

Expand Down
26 changes: 10 additions & 16 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@


@overload
def expand_type(typ: CallableType, env: Mapping[TypeVarId, Type]) -> CallableType:
...
def expand_type(typ: CallableType, env: Mapping[TypeVarId, Type]) -> CallableType: ...


@overload
def expand_type(typ: ProperType, env: Mapping[TypeVarId, Type]) -> ProperType:
...
def expand_type(typ: ProperType, env: Mapping[TypeVarId, Type]) -> ProperType: ...


@overload
def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
...
def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type: ...


def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
Expand All @@ -72,18 +69,15 @@ def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:


@overload
def expand_type_by_instance(typ: CallableType, instance: Instance) -> CallableType:
...
def expand_type_by_instance(typ: CallableType, instance: Instance) -> CallableType: ...


@overload
def expand_type_by_instance(typ: ProperType, instance: Instance) -> ProperType:
...
def expand_type_by_instance(typ: ProperType, instance: Instance) -> ProperType: ...


@overload
def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
...
def expand_type_by_instance(typ: Type, instance: Instance) -> Type: ...


def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
Expand Down Expand Up @@ -293,6 +287,8 @@ def visit_unpack_type(self, t: UnpackType) -> Type:
def expand_unpack(self, t: UnpackType) -> list[Type]:
assert isinstance(t.type, TypeVarTupleType)
repl = get_proper_type(self.variables.get(t.type.id, t.type))
if isinstance(repl, UnpackType):
repl = get_proper_type(repl.type)
if isinstance(repl, TupleType):
return repl.items
elif (
Expand Down Expand Up @@ -480,13 +476,11 @@ def expand_types(self, types: Iterable[Type]) -> list[Type]:


@overload
def expand_self_type(var: Var, typ: ProperType, replacement: ProperType) -> ProperType:
...
def expand_self_type(var: Var, typ: ProperType, replacement: ProperType) -> ProperType: ...


@overload
def expand_self_type(var: Var, typ: Type, replacement: Type) -> Type:
...
def expand_self_type(var: Var, typ: Type, replacement: Type) -> Type: ...


def expand_self_type(var: Var, typ: Type, replacement: Type) -> Type:
Expand Down
21 changes: 10 additions & 11 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,9 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
# Check IfStmt block to determine if function overloads can be merged
if_overload_name = self._check_ifstmt_for_overloads(stmt, current_overload_name)
if if_overload_name is not None:
(
if_block_with_overload,
if_unknown_truth_value,
) = self._get_executable_if_block_with_overloads(stmt)
(if_block_with_overload, if_unknown_truth_value) = (
self._get_executable_if_block_with_overloads(stmt)
)

if (
current_overload_name is not None
Expand Down Expand Up @@ -911,9 +910,11 @@ def do_func_def(
# PEP 484 disallows both type annotations and type comments
self.fail(message_registry.DUPLICATE_TYPE_SIGNATURES, lineno, n.col_offset)
arg_types = [
a.type_annotation
if a.type_annotation is not None
else AnyType(TypeOfAny.unannotated)
(
a.type_annotation
if a.type_annotation is not None
else AnyType(TypeOfAny.unannotated)
)
for a in args
]
else:
Expand Down Expand Up @@ -1790,12 +1791,10 @@ def invalid_type(self, node: AST, note: str | None = None) -> RawExpressionType:
)

@overload
def visit(self, node: ast3.expr) -> ProperType:
...
def visit(self, node: ast3.expr) -> ProperType: ...

@overload
def visit(self, node: AST | None) -> ProperType | None:
...
def visit(self, node: AST | None) -> ProperType | None: ...

def visit(self, node: AST | None) -> ProperType | None:
"""Modified visit -- keep track of the stack of nodes"""
Expand Down
6 changes: 2 additions & 4 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,11 @@ def trivial_join(s: Type, t: Type) -> Type:
@overload
def join_types(
s: ProperType, t: ProperType, instance_joiner: InstanceJoiner | None = None
) -> ProperType:
...
) -> ProperType: ...


@overload
def join_types(s: Type, t: Type, instance_joiner: InstanceJoiner | None = None) -> Type:
...
def join_types(s: Type, t: Type, instance_joiner: InstanceJoiner | None = None) -> Type: ...


def join_types(s: Type, t: Type, instance_joiner: InstanceJoiner | None = None) -> Type:
Expand Down
2 changes: 0 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ def infer_python_executable(options: Options, special_opts: argparse.Namespace)


class CapturableArgumentParser(argparse.ArgumentParser):

"""Override ArgumentParser methods that use sys.stdout/sys.stderr directly.
This is needed because hijacking sys.std* is not thread-safe,
Expand Down Expand Up @@ -396,7 +395,6 @@ def error(self, message: str) -> NoReturn:


class CapturableVersionAction(argparse.Action):

"""Supplement CapturableArgumentParser to handle --version.
This is nearly identical to argparse._VersionAction except,
Expand Down
3 changes: 1 addition & 2 deletions mypy/metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def commit(self) -> None:
"""

@abstractmethod
def list_all(self) -> Iterable[str]:
...
def list_all(self) -> Iterable[str]: ...


def random_string() -> str:
Expand Down
12 changes: 6 additions & 6 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3287,13 +3287,13 @@ def serialize(self) -> JsonDict:
"declared_metaclass": (
None if self.declared_metaclass is None else self.declared_metaclass.serialize()
),
"metaclass_type": None
if self.metaclass_type is None
else self.metaclass_type.serialize(),
"metaclass_type": (
None if self.metaclass_type is None else self.metaclass_type.serialize()
),
"tuple_type": None if self.tuple_type is None else self.tuple_type.serialize(),
"typeddict_type": None
if self.typeddict_type is None
else self.typeddict_type.serialize(),
"typeddict_type": (
None if self.typeddict_type is None else self.typeddict_type.serialize()
),
"flags": get_flags(self, TypeInfo.FLAGS),
"metadata": self.metadata,
"slots": sorted(self.slots) if self.slots is not None else None,
Expand Down
16 changes: 10 additions & 6 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ def serialize(self) -> JsonDict:
"init": self.init,
"kw_only": self.kw_only,
"has_converter": self.converter is not None,
"converter_init_type": self.converter.init_type.serialize()
if self.converter and self.converter.init_type
else None,
"converter_init_type": (
self.converter.init_type.serialize()
if self.converter and self.converter.init_type
else None
),
"context_line": self.context.line,
"context_column": self.context.column,
"init_type": self.init_type.serialize() if self.init_type else None,
Expand Down Expand Up @@ -1073,9 +1075,11 @@ def _meet_fields(types: list[Mapping[str, Type]]) -> Mapping[str, Type]:
field_to_types[name].append(typ)

return {
name: get_proper_type(reduce(meet_types, f_types))
if len(f_types) == len(types)
else UninhabitedType()
name: (
get_proper_type(reduce(meet_types, f_types))
if len(f_types) == len(types)
else UninhabitedType()
)
for name, f_types in field_to_types.items()
}

Expand Down

0 comments on commit 9d6f56f

Please sign in to comment.