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

Feature/add_type_information #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
File renamed without changes.
33 changes: 33 additions & 0 deletions deprecation/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Stubs file for `deprecation.py`.

Created using mypy's `stubgen`, types manually corrected.
"""

import datetime
from packaging import version
from typing import Callable, Optional, Union

message_location: str

class DeprecatedWarning(DeprecationWarning):
function: str
deprecated_in: Union[datetime.date, version.Version, None]
removed_in: Union[datetime.date, version.Version, None]
details: Union[datetime.date, version.Version, None]
Comment on lines +14 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples in the readme pass str for deprecated_in, removed_in, and details. I don't think details is ever a date/version.

Copy link

@nhairs-lumin nhairs-lumin Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the code supports datetime.date.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, removed_in can be a date. I don't see any indication that the other two fields can be a date.

Based on the code and docstrings, it looks like:

  • deprecated_in: version, str, or None
  • removed_in: date, version, str, or None
  • details: str or None

def __init__(
self,
function: str,
deprecated_in: Union[datetime.date, version.Version, None],
removed_in: Union[datetime.date, version.Version, None],
details: str = ...,
) -> None: ...

class UnsupportedWarning(DeprecatedWarning): ...

def deprecated(
deprecated_in: Union[str, datetime.date, version.Version, None] = ...,
removed_in: Union[str, datetime.date, version.Version, None] = ...,
current_version: Union[str, datetime.date, version.Version, None] = ...,
details: str = ...,
) -> Callable: ...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a TypeVar for the wrapped function so that the decorator's type hints preserve the function's signature. Pylance (in VS Code) manages to display the signature without this (perhaps due to the implementation's use of @functools.wraps?), but mypy shows Any if I use reveal_type on a deprecated function.

Example: https://github.com/python/typeshed/blob/main/stdlib/contextlib.pyi#L34

_F = TypeVar("_F", bound=Callable[..., Any])

def deprecated(...) -> Callable[[_F], _F]: ...
def fail_if_not_removed(method: _F) -> _F: ...

def fail_if_not_removed(method: Callable) -> Callable: ...
2 changes: 2 additions & 0 deletions deprecation/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Marker file for PEP 561. automeas uses inline type hints.
# https://peps.python.org/pep-0561/
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def _read_file():
with open("deprecation.py", "r") as f:
with open("deprecation/__init__.py", "r") as f:
return f.read()


Expand All @@ -34,7 +34,7 @@ def get_version():
install_requires=["packaging"],
keywords=["deprecation"],
long_description=io.open("README.rst", encoding="utf-8").read(),
py_modules=["deprecation"],
packages=["deprecation"],
classifiers=[
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: Apache Software License",
Expand All @@ -55,4 +55,7 @@ def get_version():
"Documentation": "http://deprecation.readthedocs.io/en/latest/",
"Source": "https://github.com/briancurtin/deprecation",
"Bug Tracker": "https://github.com/briancurtin/deprecation/issues"},
package_data = {
"deprecation": ["py.typed", "__init__.pyi"],
},
)