0

I have a circular dependency caused by type-hinting analogous to this minimal example:

a.py:

from comparison import are_equal

class A:
    def __eq__(self, other):
        if not isinstance(other, A):
            return NotImplemented

        return are_equal(self, other)

comparison.py:

from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from a import A

def are_equal(x: A, y: A):
    ...

While I would expect everything to work this way, I get the following error messages pointing to the last line of "a.py":

Argument of type "Self@A" is cannot be assigned to parameter "x" of type "A" in function "are_equal"

".A" is incompatible with "a.A"

Argument of type "A" is cannot be assigned to parameter "y" of type "A" in function "are_equal"

".A" is incompatible with "a.A"

Is there any way to fix this issue besides just opting out of type-checking by using # type: ignore on that line?


Edit:

This does not seem to be a general issue but specific to my editor setup. I am using doom emacs and both files (a.py, comparison.py) are generated (tangled) from a single .org file. The error is shown when editing the source code block containing a.py in the special editing buffer (org-edit-src-code).

Drew
  • 29,895
  • 7
  • 74
  • 104
cymin
  • 83
  • 8
  • 1
    I cannot reproduce this neither with `mypy` nor with `pyright`. Both checkers give no errors on your sample (2 files in same directory). I'm using `mypy=0.950` and `pyright 1.1.248`, both from PyPi. – STerliakov May 23 '22 at 18:26
  • Your comment made me realize that the problem is indeed specific to my editor setup. See my edit to the question. Thanks! – cymin May 24 '22 at 10:55
  • 1
    I suppose that the problem is with import machinery. Probably this new buffer is saved to `/tmp` (because `pyright` cannot work with string input, it checks only files), and then there happens name confusion (I doubt file is saved as `/tmp/a.py`), so finally import cannot be resolved and is not considered equal to `a.A`. I'm not sure you can handle it somehow, maybe file a bug report to plugin developers. – STerliakov May 24 '22 at 11:49

0 Answers0