| 522 | |
| 523 | |
| 524 | class TypeVarId: |
| 525 | # A type variable is uniquely identified by its raw id and meta level. |
| 526 | |
| 527 | # For plain variables (type parameters of generic classes and |
| 528 | # functions) raw ids are allocated by semantic analysis, using |
| 529 | # positive ids 1, 2, ... for generic class parameters and negative |
| 530 | # ids -1, ... for generic function type arguments. A special value 0 |
| 531 | # is reserved for Self type variable (autogenerated). This convention |
| 532 | # is only used to keep type variable ids distinct when allocating |
| 533 | # them; the type checker makes no distinction between class and |
| 534 | # function type variables. |
| 535 | |
| 536 | # Metavariables are allocated unique ids starting from 1. |
| 537 | raw_id: Final[int] |
| 538 | |
| 539 | # Level of the variable in type inference. Currently either 0 for |
| 540 | # declared types, or 1 for type inference metavariables. |
| 541 | meta_level: int = 0 |
| 542 | |
| 543 | # Class variable used for allocating fresh ids for metavariables. |
| 544 | next_raw_id: ClassVar[int] = 1 |
| 545 | |
| 546 | # Fullname of class or function/method which declares this type |
| 547 | # variable (not the fullname of the TypeVar definition!), or '' |
| 548 | namespace: str |
| 549 | |
| 550 | def __init__(self, raw_id: int, meta_level: int = 0, *, namespace: str = "") -> None: |
| 551 | self.raw_id = raw_id |
| 552 | self.meta_level = meta_level |
| 553 | self.namespace = namespace |
| 554 | |
| 555 | @staticmethod |
| 556 | def new(meta_level: int) -> TypeVarId: |
| 557 | raw_id = TypeVarId.next_raw_id |
| 558 | TypeVarId.next_raw_id += 1 |
| 559 | return TypeVarId(raw_id, meta_level) |
| 560 | |
| 561 | def __repr__(self) -> str: |
| 562 | return self.raw_id.__repr__() |
| 563 | |
| 564 | def __eq__(self, other: object) -> bool: |
| 565 | # Although this call is not expensive (like UnionType or TypedDictType), |
| 566 | # most of the time we get the same object here, so add a fast path. |
| 567 | if self is other: |
| 568 | return True |
| 569 | return ( |
| 570 | isinstance(other, TypeVarId) |
| 571 | and self.raw_id == other.raw_id |
| 572 | and self.meta_level == other.meta_level |
| 573 | and self.namespace == other.namespace |
| 574 | ) |
| 575 | |
| 576 | def __ne__(self, other: object) -> bool: |
| 577 | return not (self == other) |
| 578 | |
| 579 | def __hash__(self) -> int: |
| 580 | return self.raw_id ^ (self.meta_level << 8) ^ hash(self.namespace) |
| 581 |
no outgoing calls
searching dependent graphs…