diff options
author | Ran Benita <ran@unusedvar.com> | 2020-09-27 22:20:31 +0300 |
---|---|---|
committer | Ran Benita <ran@unusedvar.com> | 2020-11-13 11:25:09 +0200 |
commit | f1e6fdcddbfe8991935685ccc5049dd957ec4382 (patch) | |
tree | 854c010eef3a9accb203ccbf114b12648dc5b997 /testing/deprecated_test.py | |
parent | b0505788821604f0b0787683d47a0ca693fd0426 (diff) | |
download | pytest-f1e6fdcddbfe8991935685ccc5049dd957ec4382.tar.gz |
Export types of builtin fixture for type annotations
In order to allow users to type annotate fixtures they request, the
types need to be imported from the `pytest` namespace. They are/were
always available to import from the `_pytest` namespace, but that is
not guaranteed to be stable.
These types are only exported for the purpose of typing. Specifically,
the following are *not* public:
- Construction (`__init__`)
- Subclassing
- staticmethods and classmethods
We try to combat them being used anyway by:
- Marking the classes as `@final` when possible (already done).
- Not documenting private stuff in the API Reference.
- Using `_`-prefixed names or marking as `:meta private:` for private
stuff.
- Adding a keyword-only `_ispytest=False` to private constructors,
warning if False, and changing pytest itself to pass True. In the
future it will (hopefully) become a hard error.
Hopefully that will be enough.
Diffstat (limited to 'testing/deprecated_test.py')
-rw-r--r-- | testing/deprecated_test.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 0d1b58ad1..d213414ee 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -123,3 +123,17 @@ def test_yield_fixture_is_deprecated() -> None: @pytest.yield_fixture def fix(): assert False + + +def test_private_is_deprecated() -> None: + class PrivateInit: + def __init__(self, foo: int, *, _ispytest: bool = False) -> None: + deprecated.check_ispytest(_ispytest) + + with pytest.warns( + pytest.PytestDeprecationWarning, match="private pytest class or function" + ): + PrivateInit(10) + + # Doesn't warn. + PrivateInit(10, _ispytest=True) |