Skip to content

Commit f1eb04a

Browse files
authored
Refactor ifs into util function (#11663)
* Follow up to #11150, refactors `if`s into a util function
1 parent cee5d3e commit f1eb04a

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

mypy/checker.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
)
4747
import mypy.checkexpr
4848
from mypy.checkmember import (
49-
MemberContext, analyze_member_access, analyze_descriptor_access, analyze_var,
49+
MemberContext, analyze_member_access, analyze_descriptor_access,
5050
type_object_type,
51+
analyze_decorator_or_funcbase_access,
5152
)
5253
from mypy.typeops import (
5354
map_type_from_supertype, bind_self, erase_to_bound, make_simplified_union,
@@ -3225,15 +3226,10 @@ def check_member_assignment(self, instance_type: Type, attribute_type: Type,
32253226
if dunder_set is None:
32263227
self.fail(message_registry.DESCRIPTOR_SET_NOT_CALLABLE.format(attribute_type), context)
32273228
return AnyType(TypeOfAny.from_error), get_type, False
3228-
if isinstance(dunder_set, Decorator):
3229-
bound_method = analyze_var(
3230-
'__set__', dunder_set.var, attribute_type, attribute_type.type, mx,
3231-
)
3232-
else:
3233-
bound_method = bind_self(
3234-
function_type(dunder_set, self.named_type('builtins.function')),
3235-
attribute_type,
3236-
)
3229+
3230+
bound_method = analyze_decorator_or_funcbase_access(
3231+
defn=dunder_set, itype=attribute_type, info=attribute_type.type,
3232+
self_type=attribute_type, name='__set__', mx=mx)
32373233
typ = map_instance_to_supertype(attribute_type, dunder_set.info)
32383234
dunder_set_type = expand_type_by_instance(bound_method, typ)
32393235

mypy/checkmember.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,9 @@ def analyze_member_var_access(name: str,
398398
# the guard this search will always find object.__getattribute__ and conclude
399399
# that the attribute exists
400400
if method and method.info.fullname != 'builtins.object':
401-
if isinstance(method, Decorator):
402-
# https://github.com/python/mypy/issues/10409
403-
bound_method = analyze_var(method_name, method.var, itype, info, mx)
404-
else:
405-
bound_method = bind_self(
406-
function_type(method, mx.named_type('builtins.function')),
407-
mx.self_type,
408-
)
401+
bound_method = analyze_decorator_or_funcbase_access(
402+
defn=method, itype=itype, info=info,
403+
self_type=mx.self_type, name=method_name, mx=mx)
409404
typ = map_instance_to_supertype(itype, method.info)
410405
getattr_type = get_proper_type(expand_type_by_instance(bound_method, typ))
411406
if isinstance(getattr_type, CallableType):
@@ -423,16 +418,10 @@ def analyze_member_var_access(name: str,
423418
else:
424419
setattr_meth = info.get_method('__setattr__')
425420
if setattr_meth and setattr_meth.info.fullname != 'builtins.object':
426-
if isinstance(setattr_meth, Decorator):
427-
bound_type = analyze_var(
428-
name, setattr_meth.var, itype, info,
429-
mx.copy_modified(is_lvalue=False),
430-
)
431-
else:
432-
bound_type = bind_self(
433-
function_type(setattr_meth, mx.named_type('builtins.function')),
434-
mx.self_type,
435-
)
421+
bound_type = analyze_decorator_or_funcbase_access(
422+
defn=setattr_meth, itype=itype, info=info,
423+
self_type=mx.self_type, name=name,
424+
mx=mx.copy_modified(is_lvalue=False))
436425
typ = map_instance_to_supertype(itype, setattr_meth.info)
437426
setattr_type = get_proper_type(expand_type_by_instance(bound_type, typ))
438427
if isinstance(setattr_type, CallableType) and len(setattr_type.arg_types) > 0:
@@ -493,15 +482,9 @@ def analyze_descriptor_access(descriptor_type: Type,
493482
mx.context)
494483
return AnyType(TypeOfAny.from_error)
495484

496-
if isinstance(dunder_get, Decorator):
497-
bound_method = analyze_var(
498-
'__set__', dunder_get.var, descriptor_type, descriptor_type.type, mx,
499-
)
500-
else:
501-
bound_method = bind_self(
502-
function_type(dunder_get, mx.named_type('builtins.function')),
503-
descriptor_type,
504-
)
485+
bound_method = analyze_decorator_or_funcbase_access(
486+
defn=dunder_get, itype=descriptor_type, info=descriptor_type.type,
487+
self_type=descriptor_type, name='__set__', mx=mx)
505488

506489
typ = map_instance_to_supertype(descriptor_type, dunder_get.info)
507490
dunder_get_type = expand_type_by_instance(bound_method, typ)
@@ -1028,6 +1011,27 @@ def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> P
10281011
return type_object_type_from_function(t, info, method.info, fallback, is_new)
10291012

10301013

1014+
def analyze_decorator_or_funcbase_access(
1015+
defn: Union[Decorator, FuncBase],
1016+
itype: Instance,
1017+
info: TypeInfo,
1018+
self_type: Optional[Type],
1019+
name: str,
1020+
mx: MemberContext,
1021+
) -> Type:
1022+
"""Analyzes the type behind method access.
1023+
1024+
The function itself can possibly be decorated.
1025+
See: https://github.com/python/mypy/issues/10409
1026+
"""
1027+
if isinstance(defn, Decorator):
1028+
return analyze_var(name, defn.var, itype, info, mx)
1029+
return bind_self(
1030+
function_type(defn, mx.chk.named_type('builtins.function')),
1031+
original_type=self_type,
1032+
)
1033+
1034+
10311035
def is_valid_constructor(n: Optional[SymbolNode]) -> bool:
10321036
"""Does this node represents a valid constructor method?
10331037

0 commit comments

Comments
 (0)