pytest-dev / pytest-dev/pytest

Add a runtime check for exported classes which should not be subclassed

Open
#8,031 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: deprecation type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

From @bluetech: we expose some classes in our API which are not meant for subclassing. We indicate this with @final annotations but these are only effective when type-checking. The following is a suggestion how to add a runtime check.


it's quite clean to add a runtime deprecation warning, so might be worth doing in this PR:

def __init_subclass__(cls, **kwargs):
    warnings.warn(f"subclassing {cls} is deprecated", PytestDeprecationWarning)

def runtime_final(cls: T) -> T:
    cls.__init_subclass__ = __init_subclass__  # type: ignore
    return cls


...
@final
@runtime_final
class Ham:
    pass

Originally posted by @graingert in https://github.com/pytest-dev/pytest/pull/8017#discussion_r520805087

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation file or test; begin by reviewing the exported @final classes and the referenced pytest-dev/pytest#8017 discussion. Done means a runtime check is defined for subclassing classes intended not to be subclassed, with tests covering the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.