Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Restore some (questionable but established) behaviour
  • Loading branch information
ilevkivskyi committed Apr 29, 2023
commit 8d220d24fecc4b1ac1c38833b8e55007ba7fb0d9
11 changes: 9 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3647,7 +3647,7 @@ def analyze_lvalue(
has_explicit_value=has_explicit_value,
)
elif isinstance(lval, MemberExpr):
self.analyze_member_lvalue(lval, explicit_type, is_final)
self.analyze_member_lvalue(lval, explicit_type, is_final, has_explicit_value)
if explicit_type and not self.is_self_member_ref(lval):
self.fail("Type cannot be declared in assignment to non-self attribute", lval)
elif isinstance(lval, IndexExpr):
Expand Down Expand Up @@ -3824,7 +3824,9 @@ def analyze_tuple_or_list_lvalue(self, lval: TupleExpr, explicit_type: bool = Fa
has_explicit_value=True,
)

def analyze_member_lvalue(self, lval: MemberExpr, explicit_type: bool, is_final: bool) -> None:
def analyze_member_lvalue(
self, lval: MemberExpr, explicit_type: bool, is_final: bool, has_explicit_value: bool
) -> None:
"""Analyze lvalue that is a member expression.

Arguments:
Expand Down Expand Up @@ -3853,6 +3855,11 @@ def analyze_member_lvalue(self, lval: MemberExpr, explicit_type: bool, is_final:
and explicit_type
):
self.attribute_already_defined(lval.name, lval, cur_node)
if self.type.is_protocol and has_explicit_value and cur_node is not None:
# Make this variable non-abstract, it would be safer to do this only if we
# are inside __init__, but we do this always to preserve historical behaviour.
if isinstance(cur_node.node, Var):
cur_node.node.is_abstract_var = False
if (
# If the attribute of self is not defined, create a new Var, ...
node is None
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4054,3 +4054,14 @@ class P(Protocol):

[file lib.py]
class C: ...

[case testAllowDefaultConstructorInProtocols]
from typing import Protocol

class P(Protocol):
x: int
def __init__(self, x: int) -> None:
self.x = x

class C(P): ...
C(0) # OK