|
5 | 5 | from collections import OrderedDict |
6 | 6 | from typing import ( |
7 | 7 | Any, TypeVar, Dict, List, Tuple, cast, Generic, Set, Sequence, Optional, Union, Iterable, |
8 | | - NamedTuple, |
| 8 | + NamedTuple, Callable, |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | import mypy.nodes |
@@ -1500,112 +1500,112 @@ def keywords_str(self, a: Iterable[Tuple[str, Type]]) -> str: |
1500 | 1500 | ]) |
1501 | 1501 |
|
1502 | 1502 |
|
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 |
1508 | 1509 |
|
1509 | 1510 |
|
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 |
1512 | 1516 |
|
1513 | | - This class allows defining the default value for leafs to simplify the |
1514 | | - implementation of many queries. |
1515 | | - """ |
1516 | 1517 |
|
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 |
1519 | 1520 |
|
1520 | | - def __init__(self, default: bool, strategy: int) -> None: |
1521 | | - """Construct a query visitor. |
1522 | 1521 |
|
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: |
1527 | 1536 | self.default = default |
1528 | 1537 | self.strategy = strategy |
1529 | 1538 |
|
1530 | | - def visit_unbound_type(self, t: UnboundType) -> bool: |
| 1539 | + def visit_unbound_type(self, t: UnboundType) -> T: |
1531 | 1540 | return self.default |
1532 | 1541 |
|
1533 | | - def visit_type_list(self, t: TypeList) -> bool: |
| 1542 | + def visit_type_list(self, t: TypeList) -> T: |
1534 | 1543 | return self.default |
1535 | 1544 |
|
1536 | | - def visit_error_type(self, t: ErrorType) -> bool: |
| 1545 | + def visit_error_type(self, t: ErrorType) -> T: |
1537 | 1546 | return self.default |
1538 | 1547 |
|
1539 | | - def visit_any(self, t: AnyType) -> bool: |
| 1548 | + def visit_any(self, t: AnyType) -> T: |
1540 | 1549 | return self.default |
1541 | 1550 |
|
1542 | | - def visit_uninhabited_type(self, t: UninhabitedType) -> bool: |
| 1551 | + def visit_uninhabited_type(self, t: UninhabitedType) -> T: |
1543 | 1552 | return self.default |
1544 | 1553 |
|
1545 | | - def visit_none_type(self, t: NoneTyp) -> bool: |
| 1554 | + def visit_none_type(self, t: NoneTyp) -> T: |
1546 | 1555 | return self.default |
1547 | 1556 |
|
1548 | | - def visit_erased_type(self, t: ErasedType) -> bool: |
| 1557 | + def visit_erased_type(self, t: ErasedType) -> T: |
1549 | 1558 | return self.default |
1550 | 1559 |
|
1551 | | - def visit_deleted_type(self, t: DeletedType) -> bool: |
| 1560 | + def visit_deleted_type(self, t: DeletedType) -> T: |
1552 | 1561 | return self.default |
1553 | 1562 |
|
1554 | | - def visit_type_var(self, t: TypeVarType) -> bool: |
| 1563 | + def visit_type_var(self, t: TypeVarType) -> T: |
1555 | 1564 | return self.default |
1556 | 1565 |
|
1557 | | - def visit_partial_type(self, t: PartialType) -> bool: |
| 1566 | + def visit_partial_type(self, t: PartialType) -> T: |
1558 | 1567 | return self.default |
1559 | 1568 |
|
1560 | | - def visit_instance(self, t: Instance) -> bool: |
| 1569 | + def visit_instance(self, t: Instance) -> T: |
1561 | 1570 | return self.query_types(t.args) |
1562 | 1571 |
|
1563 | | - def visit_callable_type(self, t: CallableType) -> bool: |
| 1572 | + def visit_callable_type(self, t: CallableType) -> T: |
1564 | 1573 | # FIX generics |
1565 | 1574 | return self.query_types(t.arg_types + [t.ret_type]) |
1566 | 1575 |
|
1567 | | - def visit_tuple_type(self, t: TupleType) -> bool: |
| 1576 | + def visit_tuple_type(self, t: TupleType) -> T: |
1568 | 1577 | return self.query_types(t.items) |
1569 | 1578 |
|
1570 | | - def visit_typeddict_type(self, t: TypedDictType) -> bool: |
| 1579 | + def visit_typeddict_type(self, t: TypedDictType) -> T: |
1571 | 1580 | return self.query_types(t.items.values()) |
1572 | 1581 |
|
1573 | | - def visit_star_type(self, t: StarType) -> bool: |
| 1582 | + def visit_star_type(self, t: StarType) -> T: |
1574 | 1583 | return t.type.accept(self) |
1575 | 1584 |
|
1576 | | - def visit_union_type(self, t: UnionType) -> bool: |
| 1585 | + def visit_union_type(self, t: UnionType) -> T: |
1577 | 1586 | return self.query_types(t.items) |
1578 | 1587 |
|
1579 | | - def visit_overloaded(self, t: Overloaded) -> bool: |
| 1588 | + def visit_overloaded(self, t: Overloaded) -> T: |
1580 | 1589 | return self.query_types(t.items()) |
1581 | 1590 |
|
1582 | | - def visit_type_type(self, t: TypeType) -> bool: |
| 1591 | + def visit_type_type(self, t: TypeType) -> T: |
1583 | 1592 | return t.item.accept(self) |
1584 | 1593 |
|
1585 | | - def query_types(self, types: Iterable[Type]) -> bool: |
| 1594 | + def query_types(self, types: Iterable[Type]) -> T: |
1586 | 1595 | """Perform a query for a list of types. |
1587 | 1596 |
|
1588 | | - Use the strategy constant to combine the results. |
| 1597 | + Use the strategy to combine the results. |
1589 | 1598 | """ |
1590 | 1599 | if not types: |
1591 | 1600 | # Use default result for empty list. |
1592 | 1601 | 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: |
1596 | 1604 | 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 |
1609 | 1609 |
|
1610 | 1610 |
|
1611 | 1611 | def strip_type(typ: Type) -> Type: |
|
0 commit comments