Skip to content
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

New semantic analyzer: fix crash on broken overload at class scope #7181

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
elif isinstance(node, OverloadedFuncDef) and node.type is not None:
# node.type is None when there are multiple definitions of a function
# and it's decorated by something that is not typing.overload
# TODO: use a dummy Overloaded instead of AnyType in this case
# like we do in mypy.types.function_type()?
result = node.type
elif isinstance(node, TypeInfo):
# Reference to a type object.
Expand Down
2 changes: 0 additions & 2 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,6 @@ def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
self.process_final_in_overload(defn)
self.process_static_or_class_method_in_overload(defn)

self.add_symbol(defn.name(), defn, defn)

def analyze_overload_sigs_and_impl(
self,
defn: OverloadedFuncDef) -> Tuple[List[CallableType],
Expand Down
15 changes: 12 additions & 3 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2177,9 +2177,18 @@ def function_type(func: mypy.nodes.FuncBase, fallback: Instance) -> FunctionLike
return func.type
else:
# Implicit type signature with dynamic types.
# Overloaded functions always have a signature, so func must be an ordinary function.
assert isinstance(func, mypy.nodes.FuncItem), str(func)
return callable_type(func, fallback)
if isinstance(func, mypy.nodes.FuncItem):
return callable_type(func, fallback)
else:
# Broken overloads can have self.type set to None.
assert isinstance(func, mypy.nodes.OverloadedFuncDef)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to always set the overload type in the semantic analyzer. No need to do anything about this now, but consider adding a TODO comment.

any_type = AnyType(TypeOfAny.from_error)
dummy = CallableType([any_type, any_type],
[ARG_STAR, ARG_STAR2],
[None, None], any_type,
fallback,
line=func.line, is_ellipsis_args=True)
return Overloaded([dummy])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just return dummy? Add a comment explaining why.



def callable_type(fdef: mypy.nodes.FuncItem, fallback: Instance,
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -3113,3 +3113,22 @@ class A: pass
def f() -> None: pass

[targets m, m.f, __main__]

[case testNewAnalyzerNoCrashOnCustomProperty]
# flags: --ignore-missing-imports
from unimported import custom

class User:
first_name: str

@custom
def name(self) -> str:
return self.first_name

@name.setter # type: ignore
def name(self, value: str) -> None:
self.first_name = value

def __init__(self, name: str) -> None:
self.name = name # E: Cannot assign to a method \
# E: Incompatible types in assignment (expression has type "str", variable has type overloaded function)