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

"User defined generic does not accept provided constraints" error when 'Type' is used in hint #68

Open
smarie opened this issue Mar 12, 2018 · 4 comments

Comments

@smarie
Copy link

smarie commented Mar 12, 2018

PEP484 allows TypeVar to be used anywhere, not necessarily in Generic classes, see this section.

However as of today the following fails:

from enforce import runtime_validation
from typing import TypeVar, Type

T = TypeVar('T', bound=int)


@runtime_validation
class Foo:
    def foo(self, arg: Type[T]) -> T:
        return arg()

With error:

(...)
  File "C:\Miniconda3\envs\baseenv\lib\site-packages\enforce\enforcers.py", line 206, in generate_new_enforcer
    raise TypeError('User defined generic does not accept provided constraints')
TypeError: User defined generic does not accept provided constraints
@smarie
Copy link
Author

smarie commented Mar 12, 2018

Note that this can be even reduced down to

@runtime_validation
def foo(self, arg: Type[T]) -> T:
    return arg()

Apparently that's the Type[T] that causes the problem, because changing it to T makes it work.

@RussBaz
Copy link
Owner

RussBaz commented Mar 12, 2018

Is this happening in the dev branch as well? It might be related to the fact that Type construct is not properly supported yet. However, I will have a look into it later this week.

@RussBaz
Copy link
Owner

RussBaz commented Mar 12, 2018

If it is actually a problem with Type only, can you rename the issue to reflect it? Thanks.

@smarie smarie changed the title Support for TypeVar type hints inside non-Generic classes "User defined generic does not accept provided constraints" error when 'Type' is used in hint Mar 13, 2018
@smarie
Copy link
Author

smarie commented Mar 15, 2018

Please note that in early versions of typing.py module the Type symbol does not exist. Could you please make sure that it is not imported by enforce ? Otherwise new versions would break on our production environment (python 3.5.1, with no possibility to update the typing module using pip).

In all of my code to cope with this restriction I always quote Type when used in type hints, and add the following import:

try:
    from typing import Type
except ImportError:
    # normal for old versions of typing
    pass

Of course in your case you do want to reason about a type hint being the Type symbol, so I suggest the following import trick:

```python
try:
    from typing import Type
except ImportError:
    # normal for old versions of typing
    class Type:
        pass

I guess that should do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants