Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix crash on deferred supertype and setter override#18649

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ilevkivskyi merged 1 commit intopython:masterfromilevkivskyi:fix-setter-property
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
Fix crash on deferred supertype and setter override
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedFeb 10, 2025
commitc80dd2f2d12ca0c9c55a9d636a8adcb25c684fcb
22 changes: 15 additions & 7 deletionsmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2147,16 +2147,11 @@ def check_method_override_for_base_with_name(
# it can be checked for compatibility.
original_type = get_proper_type(base_attr.type)
original_node = base_attr.node
always_allow_covariant = False
if is_settable_property(defn) and (
is_settable_property(original_node) or isinstance(original_node, Var)
):
if is_custom_settable_property(defn) or (is_custom_settable_property(original_node)):
always_allow_covariant = True
self.check_setter_type_override(defn, base_attr, base)
# `original_type` can be partial if (e.g.) it is originally an
# instance variable from an `__init__` block that becomes deferred.
supertype_ready = True
if original_type is None or isinstance(original_type, PartialType):
supertype_ready = False
if self.pass_num < self.last_pass:
# If there are passes left, defer this node until next pass,
# otherwise try reconstructing the method type from available information.
Expand All@@ -2179,6 +2174,19 @@ def check_method_override_for_base_with_name(
else:
# Will always fail to typecheck below, since we know the node is a method
original_type = NoneType()

always_allow_covariant = False
if is_settable_property(defn) and (
is_settable_property(original_node) or isinstance(original_node, Var)
):
if is_custom_settable_property(defn) or (is_custom_settable_property(original_node)):
# Unlike with getter, where we try to construct some fallback type in case of
# deferral during last_pass, we can't make meaningful setter checks if the
# supertype is not known precisely.
if supertype_ready:
always_allow_covariant = True
self.check_setter_type_override(defn, base_attr, base)

if isinstance(original_node, (FuncDef, OverloadedFuncDef)):
original_class_or_static = original_node.is_class or original_node.is_static
elif isinstance(original_node, Decorator):
Expand Down
22 changes: 22 additions & 0 deletionstest-data/unit/check-classes.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8443,3 +8443,25 @@ class C:
def x(self) -> None:
pass
[builtins fixtures/property.pyi]

[case testPropertySetterSuperclassDeferred]
from typing import Callable, TypeVar

class B:
def __init__(self) -> None:
self.foo = f()

class C(B):
@property
def foo(self) -> str: ...
@foo.setter # E: Incompatible override of a setter type \
# N: (base class "B" defined the type as "str", \
# N: override has type "int")
def foo(self, x: int) -> None: ...

T = TypeVar("T")
def deco(fn: Callable[[], list[T]]) -> Callable[[], T]: ...

@deco
def f() -> list[str]: ...
[builtins fixtures/property.pyi]

[8]ページ先頭

©2009-2025 Movatter.jp