class documentation

class CreateTypeParametersForSignatures(Visitor): (source)

View In Hierarchy

Visitor for inserting type parameters into signatures. This visitor replaces re-occurring ~unknowns and class types (when necessary) with type parameters. For example, this will change 1. class ~unknown1: ... def f(x: ~unknown1) -> ~unknown1 to _T1 = TypeVar("_T1") def f(x: _T1) -> _T1 2. class Foo: def __new__(cls: Type[Foo]) -> Foo to _TFoo = TypeVar("_TFoo", bound=Foo) class Foo: def __new__(cls: Type[_TFoo]) -> _TFoo 3. class Foo: def __enter__(self) -> Foo to _TFoo = TypeVar("_TFoo", bound=Foo) class Foo: def __enter__(self: _TFoo) -> _TFoo

Method __init__ Undocumented
Method EnterClass Undocumented
Method EnterFunction Undocumented
Method EnterTypeDeclUnit Undocumented
Method LeaveClass Undocumented
Method LeaveFunction Undocumented
Method VisitSignature Potentially replace ~unknowns with type parameters, in a signature.
Method VisitTypeDeclUnit Undocumented
Constant PREFIX Undocumented
Instance Variable added_new_type_params Undocumented
Instance Variable class_name Undocumented
Instance Variable function_name Undocumented
Instance Variable parameter Undocumented
Method _NeedsClassParam Whether the signature needs a bounded type param for the class.

Inherited from Visitor:

Method Enter Undocumented
Method Leave Undocumented
Method Visit Undocumented
Class Variable old_node Undocumented
Class Variable unchecked_node_names Undocumented
Class Variable visits_all_node_types Undocumented
Instance Variable enter_functions Undocumented
Instance Variable leave_functions Undocumented
Instance Variable visit_class_names Undocumented
Instance Variable visit_functions Undocumented
Class Variable _visitor_functions_cache Undocumented
def __init__(self): (source)
def EnterClass(self, node): (source)

Undocumented

def EnterFunction(self, node): (source)

Undocumented

def EnterTypeDeclUnit(self, _): (source)

Undocumented

def LeaveClass(self, _): (source)

Undocumented

def LeaveFunction(self, _): (source)

Undocumented

def VisitSignature(self, sig): (source)

Potentially replace ~unknowns with type parameters, in a signature.

def VisitTypeDeclUnit(self, unit): (source)

Undocumented

Undocumented

Value
'_T'
added_new_type_params: bool = (source)

Undocumented

class_name = (source)

Undocumented

function_name = (source)

Undocumented

parameter = (source)

Undocumented

def _NeedsClassParam(self, sig): (source)

Whether the signature needs a bounded type param for the class. We detect the signatures (cls: Type[X][, ...]) -> X and (self: X[, ...]) -> X so that we can replace X with a bounded TypeVar. This heuristic isn't perfect; for example, in this naive copy method: class X: def copy(self): return X() we should have left X alone. But it prevents a number of false positives by enabling us to infer correct types for common implementations of __new__ and __enter__. Args: sig: A pytd.Signature. Returns: True if the signature needs a class param, False otherwise.