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

editable installs treated as untyped #868

Closed
richrines1 opened this issue Dec 28, 2023 · 4 comments · Fixed by #715
Closed

editable installs treated as untyped #868

richrines1 opened this issue Dec 28, 2023 · 4 comments · Fixed by #715
Assignees
Labels
bug Something isn't working

Comments

@richrines1
Copy link
Contributor

richrines1 commented Dec 28, 2023

What is happening?

when i use an editable install of any of the client-superstaq packages (e.g. pip install -e ./general-superstaq), mypy treats it as untyped when type-checking other packages (see example below). Non-editable installs work correctly

this has been happening to me since ~the transition to pyproject.toml, and seems to be fixed by the change in #715. However i don't fully understand what's going on so before making any changes i'd love to know if it's an issue on other systems or just a me issue

How can we reproduce the issue?

in the repo root directory, create & activate a new virtual environment, and then run

pip install -e ./general-superstaq -e ./cirq-superstaq
echo 'foo: gss.Target = "bar"' >> cirq-superstaq/cirq_superstaq/service.py
./checks/mypy_.py cirq-superstaq

What should happen?

it should raise a type error along the lines of

cirq-superstaq/cirq_superstaq/service.py:992: error: Incompatible types in assignment (expression has type "str", variable has type "Target")  [assignment]

but for me locally the check passes unless i remove the first -e

Environment

  • client-superstaq version: 0.5.4
  • Operating system: ubuntu 22.04
  • Python version: 3.11, 3.10
  • mypy version: 1.8.0

Any additional context?

No response

@saeubank
Copy link
Contributor

saeubank commented Jan 6, 2024

I am getting the correct error message on a new virtual env.
Though I also had to run pip install -e checks-superstaq/

This the output i get from the mypy check

supermarq-benchmarks/supermarq/plotting.py:80: error: Module has no attribute "RdBu"  [attr-defined]
supermarq-benchmarks/supermarq/plotting.py:268: error: Item "None" of "Figure | SubFigure | None" has no attribute "colorbar"  [union-attr]
supermarq-benchmarks/supermarq/plotting.py:334: error: Item "None" of "ndarray[Any, dtype[floating[_64Bit]]] | None" has no attribute "max"  [union-attr]
supermarq-benchmarks/supermarq/plotting.py:348: error: Item "None" of "ndarray[Any, dtype[floating[_64Bit]]] | None" has no attribute "shape"  [union-attr]
supermarq-benchmarks/supermarq/plotting.py:349: error: Item "None" of "ndarray[Any, dtype[floating[_64Bit]]] | None" has no attribute "shape"  [union-attr]
supermarq-benchmarks/supermarq/plotting.py:350: error: Value of type "ndarray[Any, dtype[floating[_64Bit]]] | None" is not indexable  [index]
supermarq-benchmarks/supermarq/plotting.py:351: error: Value of type "ndarray[Any, dtype[floating[_64Bit]]] | None" is not indexable  [index]
supermarq-benchmarks/supermarq/plotting.py:351: error: Argument 4 to "text" of "Axes" has incompatible type "**dict[str, str]"; expected "dict[str, Any] | None"  [arg-type]
supermarq-benchmarks/supermarq/plotting.py:351: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
supermarq-benchmarks/supermarq/plotting.py:351: note: Consider using "Mapping" instead, which is covariant in the value type
supermarq-benchmarks/supermarq/plotting.py:414: error: Signature of "plot" incompatible with supertype "Axes"  [override]
supermarq-benchmarks/supermarq/plotting.py:414: note:      Superclass:
supermarq-benchmarks/supermarq/plotting.py:414: note:          def plot(self, *args: float | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | str, scalex: bool = ..., scaley: bool = ..., data: Any = ..., **kwargs: Any) -> list[Line2D]
supermarq-benchmarks/supermarq/plotting.py:414: note:      Subclass:
supermarq-benchmarks/supermarq/plotting.py:414: note:          def plot(self, *args: Any, **kwargs: Any) -> None
supermarq-benchmarks/supermarq/plotting.py:434: error: Value of type "_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]" is not indexable  [index]
supermarq-benchmarks/supermarq/plotting.py:435: error: Value of type "_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]" is not indexable  [index]
supermarq-benchmarks/supermarq/plotting.py:436: error: Value of type "_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]" is not indexable  [index]
supermarq-benchmarks/supermarq/plotting.py:445: error: "RadarAxesMeta" has no attribute "theta"  [attr-defined]
supermarq-benchmarks/supermarq/plotting.py:453: error: "_gen_axes_spines" undefined in superclass  [misc]
cirq-superstaq/cirq_superstaq/service.py:989: error: Incompatible types in assignment (expression has type "str", variable has type "Target")  [assignment]
Found 15 errors in 2 files (checked 116 source files)

@richrines1
Copy link
Contributor Author

thanks @saeubank! by any chance could you also try running ./checks/mypy_.py cirq-superstaq?

i hadn't noticed before but apparently it does catch the error for me when i'm checking everything at once, but not if i'm just checking cirq-superstaq for some reason

@saeubank
Copy link
Contributor

saeubank commented Jan 8, 2024

Weird I also get the check passing if I run that

./checks/mypy_.py cirq-superstaq
Success: no issues found in 20 source files

@richrines1
Copy link
Contributor Author

ok this seems to be a known issue - see updated description in #715

saeubank pushed a commit that referenced this issue Jan 9, 2024
fixes: #868

this appears to be a known issue stemming from the use of import hooks
to comply with pep 660 - see for example
python/mypy#13392 and
pypa/setuptools#3518. What's happening is that
for editable installs `setuptools` creates files like
`.../site-packages/__editable__.general_superstaq-0.5.5.pth` which are
supposed to point python to the source directory. Previously these would
just be text files containing a single path, e.g.
```
/<...>/client-superstaq/general-superstaq
```
but after switching to pyproject.toml it becomes an executable hook,
e.g.
```python
import __editable___general_superstaq_0_5_5_finder; __editable___general_superstaq_0_5_5_finder.install()
```
where `__editable___general_superstaq_0_5_5_finder.py` is another file
saved in the site-packages directory. this breaks static analysis tools
because they won't execute the required code

afaict this pr seems to be the cleanest workaround - after these changes
setuptools seems to generate the old-style `.pth` files (i.e. text files
containing paths to the source directories), and `mypy` behaves as
expected (at least for me)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants