8000 Some cleanups · python/mypy@c5c1b76 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5c1b76

Browse files
author
Ivan Levkivskyi
committed
Some cleanups
1 parent 0af630f commit c5c1b76

File tree

3 files changed

+12
-38
lines changed

3 files changed

+12
-38
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5715,6 +5715,11 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
57155715

57165716
def visit_instance(self, t: Instance) -> Type:
57175717
if t.type.has_param_spec_type:
5718+
# We need this special-casing to preserve the possibility to store a
5719+
# generic function in an instance type. Things like
5720+
# forall T . Foo[[x: T], T]
5721+
# are not really expressible in current type system, but this looks like
5722+
# a useful feature, so let's keep it.
57185723
param_spec_index = next(
57195724
i for (i, tv) in enumerate(t.type.defn.type_vars) if isinstance(tv, ParamSpecType)
57205725
)

mypy/constraints.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,12 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
960960
for t, a, tk, ak in zip(
961961
template_args, cactual_args, template.arg_kinds, cactual.arg_kinds
962962
):
963-
# Unpack may have shifted indices.
964-
if not unpack_present:
965-
# This avoids bogus constraints like T <: P.args
966-
10000 if (
967-
tk == ARG_STAR
968-
and ak != ARG_STAR
969-
or tk == ARG_STAR2
970-
and ak != ARG_STAR2
971-
):
963+
# This avoids bogus constraints like T <: P.args
964+
if (tk == ARG_STAR and ak != ARG_STAR) or (
965+
tk == ARG_STAR2 and ak != ARG_STAR2
966+
):
967+
# Unpack may have shifted indices.
968+
if not unpack_present:
972969
continue
973970
if isinstance(a, ParamSpecType):
974971
# TODO: can we infer something useful for *T vs P?

mypy/solve.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Iterable, Sequence
77
from typing_extensions import TypeAlias as _TypeAlias
88

9-
from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints, neg_op
9+
from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints
1010
from mypy.expandtype import expand_type
1111
from mypy.graph_utils import prepare_sccs, strongly_connected_components, topsort
1212
from mypy.join import join_types
@@ -27,7 +27,6 @@
2727
UninhabitedType,
2828
UnionType,
2929
get_proper_type,
30-
remove_dups,
3130
)
3231
from mypy.typestate import type_state
3332

@@ -63,10 +62,6 @@ def solve_constraints(
6362
for c in constraints:
6463
extra_vars.extend([v.id for v in c.extra_tvars if v.id not in vars + extra_vars])
6564
originals.update({v.id: v for v in c.extra_tvars if v.id not in originals})
66-
if allow_polymorphic:
67-
# Constraints like T :> S and S <: T are semantically the same, but they are
68-
# represented differently. Normalize the constraint list w.r.t this equivalence.
69-
FBC9 constraints = normalize_constraints(constraints, vars + extra_vars)
7065

7166
# Collect a list of constraints for each type variable.
7267
cmap: dict[TypeVarId, list[Constraint]] = {tv: [] for tv in vars + extra_vars}
@@ -335,29 +330,6 @@ def is_trivial_bound(tp: ProperType) -> bool:
335330
return isinstance(tp, Instance) and tp.type.fullname == "builtins.object"
336331

337332

338-
def normalize_constraints(
339-
# TODO: delete this function?
340-
constraints: list[Constraint],
341-
vars: list[TypeVarId],
342-
) -> list[Constraint]:
343-
"""Normalize list of constraints (to simplify life for the non-linear solver).
344-
345-
This includes two things currently:
346-
* Complement T :> S by S <: T
347-
* Remove strict duplicates
348-
* Remove constrains for unrelated variables
349-
"""
350-
res = constraints.copy()
351-
for c in constraints:
352-
if (
353-
isinstance(c.target, TypeVarType)
354-
or isinstance(c.target, ParamSpecType)
355-
and not c.target.prefix.arg_types
356-
):
357-
res.append(Constraint(c.target, neg_op(c.op), c.origin_type_var))
358-
return [c for c in remove_dups(constraints) if c.type_var in vars]
359-
360-
361333
def transitive_closure(
362334
tvars: list[TypeVarId], constraints: list[Constraint]
363335
) -> tuple[Graph, Bounds, Bounds]:

0 commit comments

Comments
 (0)
0