Skip to content

Avoid using isinstance() checks with FuncBase for type narrowing #13607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
LDEF,
LITERAL_TYPE,
MDEF,
SYMBOL_FUNCBASE_TYPES,
AssertStmt,
AssignmentExpr,
AssignmentStmt,
Expand Down Expand Up @@ -2292,7 +2293,7 @@ def check_multiple_inheritance(self, typ: TypeInfo) -> None:
def determine_type_of_member(self, sym: SymbolTableNode) -> Type | None:
if sym.type is not None:
return sym.type
if isinstance(sym.node, FuncBase):
if isinstance(sym.node, SYMBOL_FUNCBASE_TYPES):
return self.function_type(sym.node)
if isinstance(sym.node, TypeInfo):
if sym.node.typeddict_type:
Expand Down
6 changes: 3 additions & 3 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def analyze_class_attribute_access(
t = erase_typevars(expand_type_by_instance(t, isuper))

is_classmethod = (is_decorated and cast(Decorator, node.node).func.is_class) or (
isinstance(node.node, FuncBase) and node.node.is_class
isinstance(node.node, SYMBOL_FUNCBASE_TYPES) and node.node.is_class
)
t = get_proper_type(t)
if isinstance(t, FunctionLike) and is_classmethod:
Expand Down Expand Up @@ -993,7 +993,7 @@ def analyze_class_attribute_access(
mx.not_ready_callback(name, mx.context)
return AnyType(TypeOfAny.from_error)
else:
assert isinstance(node.node, FuncBase)
assert isinstance(node.node, SYMBOL_FUNCBASE_TYPES)
typ = function_type(node.node, mx.named_type("builtins.function"))
# Note: if we are accessing class method on class object, the cls argument is bound.
# Annotated and/or explicit class methods go through other code paths above, for
Expand Down Expand Up @@ -1226,7 +1226,7 @@ def is_valid_constructor(n: SymbolNode | None) -> bool:
This includes normal functions, overloaded functions, and decorators
that return a callable type.
"""
if isinstance(n, FuncBase):
if isinstance(n, SYMBOL_FUNCBASE_TYPES):
return True
if isinstance(n, Decorator):
return isinstance(get_proper_type(n.type), FunctionLike)
Expand Down
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,7 +2936,7 @@ def get_method(self, name: str) -> FuncBase | Decorator | None:
for cls in self.mro:
if name in cls.names:
node = cls.names[name].node
if isinstance(node, FuncBase):
if isinstance(node, SYMBOL_FUNCBASE_TYPES):
return node
elif isinstance(node, Decorator): # Two `if`s make `mypyc` happy
return node
Expand Down
4 changes: 2 additions & 2 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'

from mypy.expandtype import expand_type
from mypy.nodes import (
SYMBOL_FUNCBASE_TYPES,
UNBOUND_IMPORTED,
Decorator,
FuncBase,
FuncItem,
MypyFile,
OverloadedFuncDef,
Expand Down Expand Up @@ -211,7 +211,7 @@ def snapshot_definition(node: SymbolNode | None, common: tuple[object, ...]) ->
The representation is nested tuples and dicts. Only externally
visible attributes are included.
"""
if isinstance(node, FuncBase):
if isinstance(node, SYMBOL_FUNCBASE_TYPES):
# TODO: info
if node.type:
signature = snapshot_type(node.type)
Expand Down
3 changes: 2 additions & 1 deletion mypy/server/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class 'mod.Cls'. This can also refer to an attribute inherited from a
GDEF,
LDEF,
MDEF,
SYMBOL_FUNCBASE_TYPES,
AssertTypeExpr,
AssignmentStmt,
AwaitExpr,
Expand Down Expand Up @@ -506,7 +507,7 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
if isinstance(rvalue.callee.node, TypeInfo):
# use actual __init__ as a dependency source
init = rvalue.callee.node.get("__init__")
if init and isinstance(init.node, FuncBase):
if init and isinstance(init.node, SYMBOL_FUNCBASE_TYPES):
fname = init.node.fullname
else:
fname = rvalue.callee.fullname
Expand Down