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

Create an isinstance version that works with the typing module #48

Open
erikseulean opened this issue Jul 5, 2020 · 4 comments
Open
Labels
Feature help wanted Extra attention is needed

Comments

@erikseulean
Copy link
Contributor

erikseulean commented Jul 5, 2020

Currently, the isinstance check doesn't work with anything existing in the typing module.

For example, you can't do the following:

from typing import Dict
x = {'a': 'b'}
isinstance(x, Dict[str,str])

I think this could be fairly useful, especially if you're willing to do type checks beyond the "base type" such as checking the values of the keys in a dictionary.

Since attrs-strict already has the capability to do most of these checks, how about a derived library that simply provides an alternative isinstance that works with both python types and types provided in the typing module ?

Additionally (this might already exist) it could provide a decorator for functions that checks if the passed arguments are valid based on the types specified in the function declaration.

@saytosid
Copy link
Contributor

Is there a generic enough API available that exposes the type-validation?

@erikseulean
Copy link
Contributor Author

I don't think so, or at least I don't know about it.

@saytosid
Copy link
Contributor

isinstance works with non-subscripted types -

>>> isinstance({}, Dict)
True
>>> isinstance({}, Dict[str, str])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/bb/lib/python3.7/typing.py", line 716, in __instancecheck__
    return self.__subclasscheck__(type(obj))
  File "/opt/bb/lib/python3.7/typing.py", line 724, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

I think it is a nice idea to provide an alternative that supports fully typed objects.

Instead of a derived library, How about a super library which can be used by attrs-strict and potentially many others 🙂

@gaborbernat
Copy link
Contributor

Due to lack of interest in this issue and activity I'll close this for now as won't do.

@gaborbernat gaborbernat added the help wanted Extra attention is needed label Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants