Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add provisional support for PEP 702 (marking classes and functions as deprecated) #4456

Closed
erictraut opened this issue Jan 14, 2023 · 10 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@erictraut
Copy link
Collaborator

erictraut commented Jan 14, 2023

See https://peps.python.org/pep-0702/ for details.

@erictraut erictraut added enhancement request New feature or request addressed in next version Issue is fixed and will appear in next published version labels Jan 14, 2023
@erictraut
Copy link
Collaborator Author

erictraut commented Jan 14, 2023

This will be included in the next release.

Note: @JelleZijlstra

@erictraut
Copy link
Collaborator Author

This feature includes a new diagnostic rule called reportDeprecated. It's off by default but on in strict mode. If disabled, deprecated symbol usage is displayed in VS Code using a strike-through appearance (using a diagnostic hint).

164f1d3f-f909-4e30-8acb-f186e93e690d

f9082477-20f3-4ca2-b84e-80473fd75841

@JelleZijlstra
Copy link
Contributor

Thanks as always for your fast work! Let me know if the implementation leads to any feedback about the PEP. I will also amend the PEP to note the pyright implementation.

@erictraut
Copy link
Collaborator Author

Oh, I forgot to mention that this feature will not work at runtime until the deprecated symbol is added to typing_extensions. As a temporary workaround, I've defined deprecated in the typing_extensions.pyi stub that is bundled with pyright.

@erictraut
Copy link
Collaborator Author

This is included in pyright 1.1.289, which I just published. It will also be included in a future release of pylance.

@FeeeeK
Copy link

FeeeeK commented Mar 18, 2023

Maybe I don't understand something, but it doesn't seem to work for overloads of __init__ in classes:
image
but works for function overloads
image

@erictraut
Copy link
Collaborator Author

@FeeeeK, this was an oversight. The implementation doesn't currently handle deprecated constructors. This will be addressed in the next release.

@finite-state-machine
Copy link

@FeeeeK, this was an oversight. The implementation doesn't currently handle deprecated constructors. This will be addressed in the next release.

@erictraut It looks like 1.1.373 supports deprecated __init__() @overloads 🎉, but there appear to be issues with __new__() and @overloads.

pyright-play.net

from __future__ import annotations

# pyright: strict

from typing import (
        Union,
        overload,
        )

from typing_extensions import (
        Self,
        deprecated,
        )


class ClassWithInit:

    @overload
    def __init__(self, value: int, /) -> None: ...
    @deprecated("passing strings is deprecated")
    @overload
    def __init__(self, value: str, /) -> None: ...

    def __init__(self, value: Union[int, str], /) -> None:
        pass

class ClassWithNew:

    @overload
    def __new__(cls, value: int, /) -> Self: ...
    @deprecated("passing strings is deprecated")
    @overload
    def __new__(cls, value: str, /) -> Self: ...

    def __new__(cls, value: Union[int, str], /) -> Self:
        return super().__new__(cls)


ClassWithInit(42)           # no error, as expected
ClassWithInit('forty-two')  # "The constructor for class "ClassWithInit" is deprecated"
ClassWithNew(42)            # no error, as expected
ClassWithNew('forty-two')   # no error (expected: similar error to above)

@erictraut
Copy link
Collaborator Author

Could you please file a new bug so this doesn't get lost?

@finite-state-machine
Copy link

Could you please file a new bug so this doesn't get lost?

Hopefully this will do the trick: #8574

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants