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

Class TypeConverter

mypy/fastparse.py:1874–2167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1872
1873
1874class TypeConverter:
1875 def __init__(
1876 self,
1877 errors: Errors | None,
1878 line: int = -1,
1879 override_column: int = -1,
1880 is_evaluated: bool = True,
1881 ) -> None:
1882 self.errors = errors
1883 self.line = line
1884 self.override_column = override_column
1885 self.node_stack: list[AST] = []
1886 self.is_evaluated = is_evaluated
1887
1888 def convert_column(self, column: int) -> int:
1889 """Apply column override if defined; otherwise return column.
1890
1891 Column numbers are sometimes incorrect in the AST and the column
1892 override can be used to work around that.
1893 """
1894 if self.override_column < 0:
1895 return column
1896 else:
1897 return self.override_column
1898
1899 def invalid_type(self, node: AST, note: str | None = None) -> RawExpressionType:
1900 """Constructs a type representing some expression that normally forms an invalid type.
1901 For example, if we see a type hint that says "3 + 4", we would transform that
1902 expression into a RawExpressionType.
1903
1904 The semantic analysis layer will report an "Invalid type" error when it
1905 encounters this type, along with the given note if one is provided.
1906
1907 See RawExpressionType's docstring for more details on how it's used.
1908 """
1909 return RawExpressionType(
1910 None, "typing.Any", line=self.line, column=getattr(node, "col_offset", -1), note=note
1911 )
1912
1913 @overload
1914 def visit(self, node: ast3.expr) -> ProperType: ...
1915
1916 @overload
1917 def visit(self, node: AST | None) -> ProperType | None: ...
1918
1919 def visit(self, node: AST | None) -> ProperType | None:
1920 """Modified visit -- keep track of the stack of nodes"""
1921 if node is None:
1922 return None
1923 self.node_stack.append(node)
1924 try:
1925 method = "visit_" + node.__class__.__name__
1926 visitor = getattr(self, method, None)
1927 if visitor is not None:
1928 typ = visitor(node)
1929 assert isinstance(typ, ProperType)
1930 return typ
1931 else:

Callers 5

parse_type_commentFunction · 0.85
do_func_defMethod · 0.85
make_argumentMethod · 0.85
translate_type_paramsMethod · 0.85
visit_AnnAssignMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…