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

Error importing plugin "classes.contrib.mypy.classes_plugin" cannot import name 'union_items' from 'mypy.types' #481

Open
zhukovgreen opened this issue Mar 14, 2023 · 5 comments · May be fixed by #483
Labels
bug Something isn't working

Comments

@zhukovgreen
Copy link

Bug report

What's wrong

I am hitting the error on a clean installation with python3.11 on MacOS.

Python 3.11.1 (main, Dec 12 2022, 14:17:59) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import classes.contrib.mypy.classes_plugin
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/uname/PycharmProjects/sandbox/.venv/lib/python3.11/site-packages/classes/contrib/mypy/classes_plugin.py", line 33, in <module>
    from classes.contrib.mypy.features import associated_type, supports, typeclass
  File "/Users/uname/PycharmProjects/sandbox/.venv/lib/python3.11/site-packages/classes/contrib/mypy/features/typeclass.py", line 17, in <module>
    from classes.contrib.mypy.typeops import (
  File "/Users/uname/PycharmProjects/sandbox/.venv/lib/python3.11/site-packages/classes/contrib/mypy/typeops/call_signatures.py", line 9, in <module>
    from mypy.types import TypeVarType, union_items
ImportError: cannot import name 'union_items' from 'mypy.types' (/Users/uname/PycharmProjects/sandbox/.venv/lib/python3.11/site-packages/mypy/types.cpython-311-darwin.so)

How is that should be

No errors

System information

  • python version:
  • classes version:
  • mypy version:
@zhukovgreen zhukovgreen added the bug Something isn't working label Mar 14, 2023
zhukovgreen added a commit to zhukovgreen/classes that referenced this issue Mar 16, 2023
@dmalt
Copy link

dmalt commented Jul 6, 2023

Any workaround for this one? I've tried different mypy versions but mypy-plugin doesn't want to work with any of them.

UPD.
The plugin does seem to work for mypy version < 0.981 and python < 3.10.7, but mypy this old stops to recognise phantom types. It throws Internal error: attempted lookup of unknown name [misc] for phantom types at me.

Also I'm getting a mypy error when calling the function I'm trying to overload
Argument 1 to "read" has incompatible type "Path"; expected <nothing>

All in all, I wasn't able to make mypy-plugin to work, and it kinda ruins the whole type-safety idea of classes for me. Maybe I'm missing something?

UPD2.
That's weird but mypy-plugin starts to work as expected as soon as I remove __init__.py from my package.

@dmalt
Copy link

dmalt commented Jul 7, 2023

Ok, after downgrading python and mypy my problems boiled down to this minimal example:

(test_classes) $ mypy --version
mypy 0.971 (compiled: yes)

(test_classes) $ python --version
Python 3.10.6

(test_classes) $ ll  # 
total 16
-rw-r--r--  1 ***  ***    83B Jul  6 21:22 setup.cfg
-rw-r--r--  1 ***  ***   438B Jul  7 16:50 try_classes.py

(test_classes) $ cat setup.cfg
# In setup.cfg or mypy.ini:
[mypy]
plugins =
  classes.contrib.mypy.classes_plugin

(test_classes) $ cat try_classes.py
from classes import typeclass

@typeclass
def to_json(instance) -> str:
    """That's a definition!"""

@to_json.instance(str)
def _to_json_str(instance: str) -> str:
    return '"{0}"'.format(instance)

@to_json.instance(None)
def _to_json_none(instance: None) -> str:
    return "null"

assert to_json("text") == '"text"'
assert to_json(None) == "null"

reveal_type(to_json)

(test_classes) $ mypy try_classes.py
try_classes.py:22: note: Revealed type is "classes._typeclass._TypeClass[Union[None, builtins.str], def (instance: Any) -> builtins.str, <nothing>, Literal['try_classes.to_json']]"
Success: no issues found in 1 source file

(test_classes) $ touch __init__.py

~/code/python/test_classes
(test_classes) $ mypy try_classes.py
try_classes.py:9: error: Internal error: attempted lookup of unknown name
try_classes.py:14: error: Internal error: attempted lookup of unknown name
try_classes.py:19: error: Argument 1 to "to_json" has incompatible type "str"; expected <nothing>
try_classes.py:20: error: Argument 1 to "to_json" has incompatible type "None"; expected <nothing>
try_classes.py:22: note: Revealed type is "classes._typeclass._TypeClass[<nothing>, def (instance: Any) -> builtins.str, <nothing>, Literal['test_classes.try_classes.to_json']]"
Found 4 errors in 1 file (checked 1 source file)

I'm launching on MacOS Ventura 13.4.1. As you see, adding __init__.py breaks the typechecking.

@eineruntervielen
Copy link

any updates on this issue?

@jellyfish-tech
Copy link

jellyfish-tech commented Nov 1, 2023

Same problem here, no solution found in documentation, is there any workaround?

@zhukovgreen
Copy link
Author

The solution so far to hard pin mypy==0.971

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

4 participants