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

Annotated[Any, ...] #12757

Closed
orenbenkiki opened this issue May 9, 2022 · 4 comments
Closed

Annotated[Any, ...] #12757

orenbenkiki opened this issue May 9, 2022 · 4 comments
Labels

Comments

@orenbenkiki
Copy link

Feature

Id like to be able to write:

SeriesOfBool = NewType("SeriesOfBool", Annotated[pandas.Series, "bool"])

def is_series_of_bool(series: pandas.Series) -> TypeGuard[SeriesOfBool]:
    return series.dtype == "bool"

Pitch

I would dearly love to track the data type of the many series values floating around my code. The above seems like a reasonable approach., which seems to work fine if I define ArrayOfBool = NewType("ArrayOfBool", Annotated[numpy.ndarray, "bool"]).

However because pandas has no type annotations, then pandas.Series is essentially Any and trying the above gives the error Argument 2 to NewType(...) must be subclassable (got "Any").

It seems like Annotated[Any, ...] shouldn't be the same as Any - in particular, not all values belong to the annotated type - so this error is over conservative?

@Viicos
Copy link
Contributor

Viicos commented Nov 30, 2022

@orenbenkiki pandas has no type annotations but a stubs package is available.
mypy should probably warn you about missing types, you can use the --install-types option.

@orenbenkiki
Copy link
Author

@Viicos - Thanks, I'll look into it. The point still stands; it seems that Anotated[Any, ...] shouldn't be the same as Any, but mypy treats them in the same way.

@Viicos
Copy link
Contributor

Viicos commented Nov 30, 2022

Any cannot be subclassable, and Annotated doesn't make it subclassable either. This is subject to changes since Python 3.11, as Any is now defined as a class (still, mypy doesn't seem to allow it yet, there might be an issue about this -- Python 3.11 isn't fully supported yet).

Other than that I find your implementation pretty neat, I don't recall seeing this anywhere. Maybe you could even get rid of the NewType here:

SeriesOfBool = Annotated[pandas.Series, "bool"]

def is_series_of_bool(series: pandas.Series) -> TypeGuard[SeriesOfBool]:
    return series.dtype == "bool"

Needs to be tested ;)

@hauntsaninja
Copy link
Collaborator

I'm not sure what NewType of Any is meant to do. If you want an opaque new type, you should make a NewType of object.

Annotated[T, ...] is usually exactly the same as T; I don't think it works how you think it works. From PEP 593:

If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x, it should ignore it and simply treat the type as T.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2022
@Viicos Viicos mentioned this issue Nov 30, 2022
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants