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

Method is_alias_expression

mypy/stubgen.py:1150–1203  ·  view source on GitHub ↗

Return True for things that look like target for an alias. Used to know if assignments look like type aliases, function alias, or module alias.

(self, expr: Expression, top_level: bool = True)

Source from the content-addressed store, hash-verified

1148 self._state = VAR
1149
1150 def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:
1151 """Return True for things that look like target for an alias.
1152
1153 Used to know if assignments look like type aliases, function alias,
1154 or module alias.
1155 """
1156 # Assignment of TypeVar(...) and other typevar-likes are passed through
1157 if isinstance(expr, CallExpr) and self.get_fullname(expr.callee) in TYPE_VAR_LIKE_NAMES:
1158 return True
1159 elif isinstance(expr, EllipsisExpr):
1160 return not top_level
1161 elif isinstance(expr, NameExpr):
1162 if expr.name in ("True", "False"):
1163 return False
1164 elif expr.name == "None":
1165 return not top_level
1166 else:
1167 return not self.is_private_name(expr.name)
1168 elif isinstance(expr, MemberExpr) and self.analyzed:
1169 # Also add function and module aliases.
1170 return (
1171 top_level
1172 and isinstance(expr.node, (FuncDef, Decorator, MypyFile))
1173 or isinstance(expr.node, TypeInfo)
1174 ) and not self.is_private_member(expr.node.fullname)
1175 elif isinstance(expr, IndexExpr) and (
1176 (isinstance(expr.base, NameExpr) and not self.is_private_name(expr.base.name))
1177 or ( # Also some known aliases that could be member expression
1178 isinstance(expr.base, MemberExpr)
1179 and not self.is_private_member(get_qualified_name(expr.base))
1180 and self.get_fullname(expr.base).startswith(
1181 ("builtins.", "typing.", "typing_extensions.", "collections.abc.")
1182 )
1183 )
1184 ):
1185 if isinstance(expr.index, TupleExpr):
1186 indices = expr.index.items
1187 else:
1188 indices = [expr.index]
1189 if expr.base.name == "Callable" and len(indices) == 2:
1190 args, ret = indices
1191 if isinstance(args, EllipsisExpr):
1192 indices = [ret]
1193 elif isinstance(args, ListExpr):
1194 indices = args.items + [ret]
1195 else:
1196 return False
1197 return all(self.is_alias_expression(i, top_level=False) for i in indices)
1198 elif isinstance(expr, OpExpr) and expr.op == "|":
1199 return self.is_alias_expression(
1200 expr.left, top_level=False
1201 ) and self.is_alias_expression(expr.right, top_level=False)
1202 else:
1203 return False
1204
1205 def process_typealias(
1206 self, lvalue: NameExpr, rvalue: Expression, is_explicit_type_alias: bool = False

Callers 1

visit_assignment_stmtMethod · 0.95

Calls 8

get_fullnameMethod · 0.95
is_private_memberMethod · 0.95
isinstanceFunction · 0.85
get_qualified_nameFunction · 0.85
lenFunction · 0.85
allFunction · 0.85
is_private_nameMethod · 0.80
startswithMethod · 0.45

Tested by

no test coverage detected