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

[stubtest] Respect @final runtime decorator, enforce it in stubs #14922

Merged
merged 2 commits into from
Mar 19, 2023
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
Next Next commit
[stubtest] Respect @final runtime decorator, enforce it in stubs
  • Loading branch information
sobolevn committed Mar 18, 2023
commit f7790f9d89b01a57407bc645b50bf57b9075f02d
10 changes: 10 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,16 @@ class SubClass(runtime): # type: ignore[misc]
# Examples: ctypes.Array, ctypes._SimpleCData
pass

# Runtime class might be annotated with `@final`:
if getattr(runtime, "__final__", False) and not stub.is_final:
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
yield Error(
object_path,
"has `__final__` attribute, but isn't marked with @final in the stub",
stub,
runtime,
stub_desc=repr(stub),
)


def _verify_metaclass(
stub: nodes.TypeInfo, runtime: type[Any], object_path: list[str]
Expand Down
39 changes: 39 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,45 @@ def test_not_subclassable(self) -> Iterator[Case]:
error="CannotBeSubclassed",
)

@collect_cases
def test_has_runtime_final_decorator(self) -> Iterator[Case]:
yield Case(
stub="from typing_extensions import final",
runtime="from typing_extensions import final",
error=None,
)
yield Case(
stub="""
@final
class A: ...
""",
runtime="""
@final
class A: ...
""",
error=None,
)
yield Case( # Runtime can miss `@final` decorator
stub="""
@final
class B: ...
""",
runtime="""
class B: ...
""",
error=None,
)
yield Case( # Stub cannot miss `@final` decorator
stub="""
class C: ...
""",
runtime="""
@final
class C: ...
""",
error="C",
)

@collect_cases
def test_name_mangling(self) -> Iterator[Case]:
yield Case(
Expand Down