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

typing_extensions.Self not working as expected #96

Closed
adamjstewart opened this issue Nov 18, 2022 · 5 comments
Closed

typing_extensions.Self not working as expected #96

adamjstewart opened this issue Nov 18, 2022 · 5 comments

Comments

@adamjstewart
Copy link

I'm attempting to use typing_extensions.Self but finding that it isn't working as expected. I modified the example code here. First, create a test.py file containing:

from typing_extensions import Self

class Foo:
    a = 2
    def return_self(self) -> Self:
        return self

x = Foo()
reveal_type(x)
reveal_type(x.a)
y = x.return_self()
reveal_type(y)
reveal_type(y.a)

Then run:

$ mypy test.py
test.py:5: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
test.py:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
test.py:9: note: Revealed type is "test.Foo"
test.py:10: note: Revealed type is "builtins.int"
test.py:12: note: Revealed type is "Self?"
test.py:13: error: Self? has no attribute "a"  [attr-defined]
test.py:13: note: Revealed type is "Any"
Found 2 errors in 1 file (checked 1 source file)

A few things seem wrong to me:

  1. Why is typing_extensions.Self not valid as a type?
  2. Why is the revealed type of y Self? and not test.Foo?
  3. Why does mypy complain when I try to access y.a?

Some useful versions to reproduce this:

Python: 3.10.8
typing-extensions: 4.3.0

@hauntsaninja
Copy link
Collaborator

This technically isn't anything to do with the runtime implementation of typing_extensions (which is what this repo is).

mypy just doesn't yet know how to interpret the Self type hint. The next release of mypy should.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 18, 2022

For now you can do:

Self = TypeVar("Self")
class Foo:
    def return_self(self: Self) -> Self: ...

(note that self needs an additional annotation here)

@adamjstewart
Copy link
Author

Thanks for the clarification!

@liu123liu123liu
Copy link

liu123liu123liu commented Mar 26, 2024

Traceback (most recent call last):
  File "/tmp/pycharm_project_710/cnn/train.py", line 23, in <module>
    from model.crn import CRN
  File "/tmp/pycharm_project_710/cnn/model/crn.py", line 3, in <module>
    from torch_geometric.nn import GCNConv
  File "/root/miniconda3/lib/python3.8/site-packages/torch_geometric/__init__.py", line 9, in <module>
    import torch_geometric.data
  File "/root/miniconda3/lib/python3.8/site-packages/torch_geometric/data/__init__.py", line 5, in <module>
    from .data import Data
  File "/root/miniconda3/lib/python3.8/site-packages/torch_geometric/data/data.py", line 469, in <module>
    class Data(BaseData, FeatureStore, GraphStore):
  File "/root/miniconda3/lib/python3.8/site-packages/torch_geometric/data/data.py", line 641, in Data
    def update(self, data: Union[Self, Dict[str, Any]]) -> Self:
  File "/root/miniconda3/lib/python3.8/typing.py", line 261, in inner
    return func(*args, **kwds)
  File "/root/miniconda3/lib/python3.8/typing.py", line 358, in __getitem__
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/root/miniconda3/lib/python3.8/typing.py", line 358, in <genexpr>
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/root/miniconda3/lib/python3.8/typing.py", line 145, in _type_check
    raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing_extensions.Self is not valid as type argument

Do you know how to slove the typeerror?

@AlexWaygood
Copy link
Member

@liu123liu123liu, it's pretty impossible to tell you how you could "solve" this error without a minimal reproducible example. Even if we had one, it doesn't seem like this error has anything to do with the original issue reported here. If you are able to create a minimal, reproducible example and you're confident that it demonstrates a bug in typing_extensions rather than a bug in your code or another library, feel free to open a new issue :-)

@python python locked as resolved and limited conversation to collaborators Mar 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants