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

Stubtest: verify stub methods or properties are decorated with@final if they are decorated with@final at runtime#14951

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
sobolevn merged 7 commits intopython:masterfromAlexWaygood:stubtest-final-methods
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
Also support the other order of stacking@final
  • Loading branch information
@AlexWaygood
AlexWaygood committedMar 24, 2023
commit329c1c8e8645cc83f04d08ed23c2cc901a67b44a
16 changes: 12 additions & 4 deletionsmypy/stubtest.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -953,7 +953,7 @@ def verify_funcitem(
if isinstance(stub, nodes.FuncDef):
for error_text in _verify_abstract_status(stub, runtime):
yield Error(object_path, error_text, stub, runtime)
for error_text in _verify_final_method(stub, static_runtime):
for error_text in _verify_final_method(stub,runtime,static_runtime):
yield Error(object_path, error_text, stub, runtime)

for message in _verify_static_class_methods(stub, runtime, static_runtime, object_path):
Expand DownExpand Up@@ -1125,7 +1125,7 @@ def verify_paramspecexpr(
def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
assert stub.func.is_property
if isinstance(runtime, property):
yield from _verify_final_method(stub.func, runtime.fget)
yield from _verify_final_method(stub.func, runtime.fget, MISSING)
return
if inspect.isdatadescriptor(runtime):
# It's enough like a property...
Expand DownExpand Up@@ -1154,8 +1154,16 @@ def _verify_abstract_status(stub: nodes.FuncDef, runtime: Any) -> Iterator[str]:
yield f"is inconsistent, runtime {item_type} is abstract but stub is not"


def _verify_final_method(stub: nodes.FuncDef, runtime: Any) -> Iterator[str]:
if getattr(runtime, "__final__", False) and not stub.is_final:
def _verify_final_method(stub: nodes.FuncDef, runtime: Any, static_runtime: MaybeMissing[Any]) -> Iterator[str]:
if stub.is_final:
return
if (
getattr(runtime, "__final__", False)
or (
static_runtime is not MISSING
and getattr(static_runtime, "__final__", False)
)
):
yield "is decorated with @final at runtime, but not in the stub"


Expand Down
44 changes: 36 additions & 8 deletionsmypy/test/teststubtest.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1311,43 +1311,71 @@ def foo(): pass
yield Case(
stub="""
class H:
@staticmethod
def foo() -> None: ...
""",
runtime="""
class H:
@staticmethod
@final
def foo(): pass
""",
error="H.foo",
)
yield Case(
stub="""
class I:
@classmethod
def foo(cls) -> None: ...
""",
runtime="""
classH:
classI:
@final
@classmethod
def foo(cls): pass
""",
error="H.foo",
error="I.foo",
)
yield Case(
stub="""
class I:
class J:
@classmethod
def foo(cls) -> None: ...
""",
runtime="""
class J:
@classmethod
@final
def foo(cls): pass
""",
error="J.foo",
)
yield Case(
stub="""
class K:
@property
def foo(self) -> int: ...
""",
runtime="""
classI:
classK:
@property
@final
def foo(self): return 42
""",
error="I.foo",
error="K.foo",
)
yield Case(
stub="""
classJ:
classL:
def foo(self, obj: int) -> int: ...
""",
runtime="""
classJ:
classL:
@final
@functools.lru_cache()
def foo(self, obj): return obj * 2
""",
error="J.foo",
error="L.foo",
)

@collect_cases
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp