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

Type mismatch for typed variable return. Expected one of the following ['list[str]'] but got <class 'list'> instead. #27

Closed
creme332 opened this issue Dec 22, 2023 · 2 comments

Comments

@creme332
Copy link

creme332 commented Dec 22, 2023

I am using python 3.9.18 and type-enforced 1.2.0 and the following code causes an error:

from __future__ import annotations
import type_enforced


@type_enforced.Enforcer
class foo:
    def __init__(self):
        pass

    def bar(self) -> list[str]:
        return ["hello", "world"]


foo().bar() # error here

Error produced:

Exception has occurred: TypeError
(foo.bar): Type mismatch for typed variable `return`. Expected one of the following `['list[str]']` but got `<class 'list'>` instead.
  File "/home/mcl/githubrepos/project/src/test.py", line 15, in <module>
    print(foo().bar())
TypeError: (foo.bar): Type mismatch for typed variable `return`. Expected one of the following `['list[str]']` but got `<class 'list'>` instead.

Any help is appreciated. Thanks.

@connor-makowski
Copy link
Owner

Thanks for creating an issue. I am able to reproduce your error.

It appears that an easy fix is to remove:

from __future__ import annotations

That line modifies the way annotations are handled (they are postponed). This prevents the type_enforced package from being able to handle annotations correctly as python stores them differently under the hood.

This postponed structure will possibly be introduced as the default in python4, but we will not add support for it until it is official. Originally, this was supposed to be the case in python3.10, however it keeps getting pushed back.

Please close this issue if it is resolved.

@creme332
Copy link
Author

Thank you. I had from __future__ import annotations in my code because it fixes an error that Pylance threw when I use union types as such int | float. By removing from __future__ import annotations, I had to use another syntax for union types: [int | float].

Reference:

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