dry-python/classes

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

zhukovgreen opened this issue ยท 5 comments

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:
dmalt commented

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 commented

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.

any updates on this issue?

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

The solution so far to hard pin mypy==0.971