Skip to content

Commit 57bca93

Browse files
committed
Make TypeQuery more general, handling nonboolean queries.
Instead of TypeQuery always returning a boolean and having the strategy be an enum, the strategy is now a Callable describing how to combine partial results, and the two default strategies are plain old funcitons. To preserve the short-circuiting behavior of the previous code, this PR uses an exception. This is a pure refactor that I am using in my experimentation regarding fixing python#1551. It should result in exactly no change to current behavior. It's separable from the other things I'm experimenting with, so I'm filing it as a separate pull request now. It enables me to rewrite the code that pulls type variables out of types as a TypeQuery. Consider waiting to merge this PR until I have some code that uses it ready for review. Or merge it now, if you think it's a pleasant cleanup instead of an ugly complication. I'm of two minds on that particular question.
1 parent 757a6b8 commit 57bca93

4 files changed

Lines changed: 59 additions & 59 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ def replace_callable_return_type(c: CallableType, new_ret_type: Type) -> Callabl
23752375
return c.copy_modified(ret_type=new_ret_type)
23762376

23772377

2378-
class ArgInferSecondPassQuery(types.TypeQuery):
2378+
class ArgInferSecondPassQuery(types.TypeQuery[bool]):
23792379
"""Query whether an argument type should be inferred in the second pass.
23802380
23812381
The result is True if the type has a type variable in a callable return
@@ -2389,7 +2389,7 @@ def visit_callable_type(self, t: CallableType) -> bool:
23892389
return self.query_types(t.arg_types) or t.accept(HasTypeVarQuery())
23902390

23912391

2392-
class HasTypeVarQuery(types.TypeQuery):
2392+
class HasTypeVarQuery(types.TypeQuery[bool]):
23932393
"""Visitor for querying whether a type has a type variable component."""
23942394
def __init__(self) -> None:
23952395
super().__init__(False, types.ANY_TYPE_STRATEGY)
@@ -2402,7 +2402,7 @@ def has_erased_component(t: Type) -> bool:
24022402
return t is not None and t.accept(HasErasedComponentsQuery())
24032403

24042404

2405-
class HasErasedComponentsQuery(types.TypeQuery):
2405+
class HasErasedComponentsQuery(types.TypeQuery[bool]):
24062406
"""Visitor for querying whether a type has an erased component."""
24072407
def __init__(self) -> None:
24082408
super().__init__(False, types.ANY_TYPE_STRATEGY)

‎mypy/constraints.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def is_complete_type(typ: Type) -> bool:
250250
return typ.accept(CompleteTypeVisitor())
251251

252252

253-
class CompleteTypeVisitor(TypeQuery):
253+
class CompleteTypeVisitor(TypeQuery[bool]):
254254
def __init__(self) -> None:
255255
super().__init__(default=True, strategy=ALL_TYPES_STRATEGY)
256256

‎mypy/stats.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def is_imprecise(t: Type) -> bool:
226226
return t.accept(HasAnyQuery())
227227

228228

229-
class HasAnyQuery(TypeQuery):
229+
class HasAnyQuery(TypeQuery[bool]):
230230
def __init__(self) -> None:
231231
super().__init__(False, ANY_TYPE_STRATEGY)
232232

‎mypy/types.py‎

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import OrderedDict
66
from typing import (
77
Any, TypeVar, Dict, List, Tuple, cast, Generic, Set, Sequence, Optional, Union, Iterable,
8-
NamedTuple,
8+
NamedTuple, Callable,
99
)
1010

1111
import mypy.nodes
@@ -1500,112 +1500,112 @@ def keywords_str(self, a: Iterable[Tuple[str, Type]]) -> str:
15001500
])
15011501

15021502

1503-
# These constants define the method used by TypeQuery to combine multiple
1504-
# query results, e.g. for tuple types. The strategy is not used for empty
1505-
# result lists; in that case the default value takes precedence.
1506-
ANY_TYPE_STRATEGY = 0 # Return True if any of the results are True.
1507-
ALL_TYPES_STRATEGY = 1 # Return True if all of the results are True.
1503+
# Combination strategies for boolean type queries
1504+
def ANY_TYPE_STRATEGY(current: bool, accumulated: bool) -> bool:
1505+
"""True if any type's result is True"""
1506+
if accumulated:
1507+
raise ShortCircuitQuery()
1508+
return current
15081509

15091510

1510-
class TypeQuery(TypeVisitor[bool]):
1511-
"""Visitor for performing simple boolean queries of types.
1511+
def ALL_TYPES_STRATEGY(current: bool, accumulated: bool) -> bool:
1512+
"""True if all types' results are True"""
1513+
if not accumulated:
1514+
raise ShortCircuitQuery()
1515+
return current
15121516

1513-
This class allows defining the default value for leafs to simplify the
1514-
implementation of many queries.
1515-
"""
15161517

1517-
default = False # Default result
1518-
strategy = 0 # Strategy for combining multiple values (ANY_TYPE_STRATEGY or ALL_TYPES_...).
1518+
class ShortCircuitQuery(Exception):
1519+
pass
15191520

1520-
def __init__(self, default: bool, strategy: int) -> None:
1521-
"""Construct a query visitor.
15221521

1523-
Use the given default result and strategy for combining
1524-
multiple results. The strategy must be either
1525-
ANY_TYPE_STRATEGY or ALL_TYPES_STRATEGY.
1526-
"""
1522+
class TypeQuery(Generic[T], TypeVisitor[T]):
1523+
"""Visitor for performing queries of types.
1524+
1525+
default is used as the query result unless a method for that type is
1526+
overridden.
1527+
1528+
strategy is used to combine a partial result with a result for a particular
1529+
type in a series of types.
1530+
1531+
Common use cases involve a boolean query using ANY_TYPE_STRATEGY and a
1532+
default of False or ALL_TYPES_STRATEGY and a default of True.
1533+
"""
1534+
1535+
def __init__(self, default: T, strategy: Callable[[T, T], T]) -> None:
15271536
self.default = default
15281537
self.strategy = strategy
15291538

1530-
def visit_unbound_type(self, t: UnboundType) -> bool:
1539+
def visit_unbound_type(self, t: UnboundType) -> T:
15311540
return self.default
15321541

1533-
def visit_type_list(self, t: TypeList) -> bool:
1542+
def visit_type_list(self, t: TypeList) -> T:
15341543
return self.default
15351544

1536-
def visit_error_type(self, t: ErrorType) -> bool:
1545+
def visit_error_type(self, t: ErrorType) -> T:
15371546
return self.default
15381547

1539-
def visit_any(self, t: AnyType) -> bool:
1548+
def visit_any(self, t: AnyType) -> T:
15401549
return self.default
15411550

1542-
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
1551+
def visit_uninhabited_type(self, t: UninhabitedType) -> T:
15431552
return self.default
15441553

1545-
def visit_none_type(self, t: NoneTyp) -> bool:
1554+
def visit_none_type(self, t: NoneTyp) -> T:
15461555
return self.default
15471556

1548-
def visit_erased_type(self, t: ErasedType) -> bool:
1557+
def visit_erased_type(self, t: ErasedType) -> T:
15491558
return self.default
15501559

1551-
def visit_deleted_type(self, t: DeletedType) -> bool:
1560+
def visit_deleted_type(self, t: DeletedType) -> T:
15521561
return self.default
15531562

1554-
def visit_type_var(self, t: TypeVarType) -> bool:
1563+
def visit_type_var(self, t: TypeVarType) -> T:
15551564
return self.default
15561565

1557-
def visit_partial_type(self, t: PartialType) -> bool:
1566+
def visit_partial_type(self, t: PartialType) -> T:
15581567
return self.default
15591568

1560-
def visit_instance(self, t: Instance) -> bool:
1569+
def visit_instance(self, t: Instance) -> T:
15611570
return self.query_types(t.args)
15621571

1563-
def visit_callable_type(self, t: CallableType) -> bool:
1572+
def visit_callable_type(self, t: CallableType) -> T:
15641573
# FIX generics
15651574
return self.query_types(t.arg_types + [t.ret_type])
15661575

1567-
def visit_tuple_type(self, t: TupleType) -> bool:
1576+
def visit_tuple_type(self, t: TupleType) -> T:
15681577
return self.query_types(t.items)
15691578

1570-
def visit_typeddict_type(self, t: TypedDictType) -> bool:
1579+
def visit_typeddict_type(self, t: TypedDictType) -> T:
15711580
return self.query_types(t.items.values())
15721581

1573-
def visit_star_type(self, t: StarType) -> bool:
1582+
def visit_star_type(self, t: StarType) -> T:
15741583
return t.type.accept(self)
15751584

1576-
def visit_union_type(self, t: UnionType) -> bool:
1585+
def visit_union_type(self, t: UnionType) -> T:
15771586
return self.query_types(t.items)
15781587

1579-
def visit_overloaded(self, t: Overloaded) -> bool:
1588+
def visit_overloaded(self, t: Overloaded) -> T:
15801589
return self.query_types(t.items())
15811590

1582-
def visit_type_type(self, t: TypeType) -> bool:
1591+
def visit_type_type(self, t: TypeType) -> T:
15831592
return t.item.accept(self)
15841593

1585-
def query_types(self, types: Iterable[Type]) -> bool:
1594+
def query_types(self, types: Iterable[Type]) -> T:
15861595
"""Perform a query for a list of types.
15871596
1588-
Use the strategy constant to combine the results.
1597+
Use the strategy to combine the results.
15891598
"""
15901599
if not types:
15911600
# Use default result for empty list.
15921601
return self.default
1593-
if self.strategy == ANY_TYPE_STRATEGY:
1594-
# Return True if at least one component is true.
1595-
res = False
1602+
res = self.default
1603+
try:
15961604
for t in types:
1597-
res = res or t.accept(self)
1598-
if res:
1599-
break
1600-
return res
1601-
else:
1602-
# Return True if all components are true.
1603-
res = True
1604-
for t in types:
1605-
res = res and t.accept(self)
1606-
if not res:
1607-
break
1608-
return res
1605+
res = self.strategy(t.accept(self), res)
1606+
except ShortCircuitQuery:
1607+
pass
1608+
return res
16091609

16101610

16111611
def strip_type(typ: Type) -> Type:

0 commit comments

Comments
 (0)