MCPcopy
hub / github.com/python/mypy / Instance

Class Instance

mypy/types.py:1593–1830  ·  view source on GitHub ↗

An instance type of form C[T1, ..., Tn]. The list of type variables may be empty. Several types have fallbacks to `Instance`, because in Python everything is an object and this concept is impossible to express without intersection types. We therefore use fallbacks for all "non-spec

Source from the content-addressed store, hash-verified

1591
1592
1593class Instance(ProperType):
1594 """An instance type of form C[T1, ..., Tn].
1595
1596 The list of type variables may be empty.
1597
1598 Several types have fallbacks to `Instance`, because in Python everything is an object
1599 and this concept is impossible to express without intersection types. We therefore use
1600 fallbacks for all "non-special" (like UninhabitedType, ErasedType etc) types.
1601 """
1602
1603 __slots__ = ("type", "args", "invalid", "type_ref", "last_known_value", "_hash", "extra_attrs")
1604
1605 def __init__(
1606 self,
1607 typ: mypy.nodes.TypeInfo,
1608 args: Sequence[Type],
1609 line: int = -1,
1610 column: int = -1,
1611 *,
1612 last_known_value: LiteralType | None = None,
1613 extra_attrs: ExtraAttrs | None = None,
1614 ) -> None:
1615 super().__init__(line, column)
1616 self.type = typ
1617 self.args = tuple(args)
1618 self.type_ref: str | None = None
1619
1620 # This field keeps track of the underlying Literal[...] value associated with
1621 # this instance, if one is known.
1622 #
1623 # This field is set whenever possible within expressions, but is erased upon
1624 # variable assignment (see erasetype.remove_instance_last_known_values) unless
1625 # the variable is declared to be final.
1626 #
1627 # For example, consider the following program:
1628 #
1629 # a = 1
1630 # b: Final[int] = 2
1631 # c: Final = 3
1632 # print(a + b + c + 4)
1633 #
1634 # The 'Instance' objects associated with the expressions '1', '2', '3', and '4' will
1635 # have last_known_values of type Literal[1], Literal[2], Literal[3], and Literal[4]
1636 # respectively. However, the Instance object assigned to 'a' and 'b' will have their
1637 # last_known_value erased: variable 'a' is mutable; variable 'b' was declared to be
1638 # specifically an int.
1639 #
1640 # Or more broadly, this field lets this Instance "remember" its original declaration
1641 # when applicable. We want this behavior because we want implicit Final declarations
1642 # to act pretty much identically with constants: we should be able to replace any
1643 # places where we use some Final variable with the original value and get the same
1644 # type-checking behavior. For example, we want this program:
1645 #
1646 # def expects_literal(x: Literal[3]) -> None: pass
1647 # var: Final = 3
1648 # expects_literal(var)
1649 #
1650 # ...to type-check in the exact same way as if we had written the program like this:

Callers 15

intersect_instancesMethod · 0.90
check_for_truthy_typeMethod · 0.90
_named_typeMethod · 0.90
named_generic_typeMethod · 0.90
instance_fallbackFunction · 0.90
meta_has_operatorFunction · 0.90
visit_enum_index_exprMethod · 0.90
split_for_callableMethod · 0.90

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…