diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 7d5af0fba6cc..21ed7e420ff4 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -551,31 +551,33 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> ): # Special hanlding for get_args(), returns a typed tuple # with the type set by the input - typ = None + argtyp = None if isinstance(e.args[0], IndexExpr): self.accept(e.args[0].index) - typ = self.chk.lookup_type(e.args[0].index) - else: + argtyp = self.chk.lookup_type(e.args[0].index) + elif isinstance(e.args[0], NameExpr): try: node = self.chk.lookup_qualified(e.args[0].name) if node: if isinstance(node.node, TypeAlias): # Resolve type - typ = get_proper_type(node.node.target) - else: - typ = node.node.type + argtyp = node.node.target + elif isinstance(node.node, Var): + argtyp = node.node.type except KeyError: # Undefined names should already be reported in semantic analysis. pass + if argtyp is not None: + argtyp = get_proper_type(argtyp) if ( - typ is not None - and isinstance(typ, UnionType) - and all(isinstance(t, LiteralType) for t in typ.items) + argtyp is not None + and isinstance(argtyp, UnionType) + and all(isinstance(get_proper_type(t), LiteralType) for t in argtyp.items) ): # Returning strings is defined but order isn't so # we need to return type * len of the union return TupleType( - [typ] * len(typ.items), fallback=self.named_type("builtins.tuple") + [argtyp] * len(argtyp.items), fallback=self.named_type("builtins.tuple") ) else: # Fall back to what we did anyway (Tuple[Any])