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

PEP673 Self type not working #14167

Closed
remdragon opened this issue Nov 23, 2022 · 2 comments
Closed

PEP673 Self type not working #14167

remdragon opened this issue Nov 23, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@remdragon
Copy link

Bug Report

New PEP 673 Self doesn't seem to work as advertised.

To Reproduce

The following example was adapted from https://docs.python.org/3/whatsnew/3.11.html#whatsnew311-pep673

from typing import Self

class MyLock:
	def __enter__(self) -> Self:
		self.lock()
		return self
	
	def lock( self ) -> None: ...

class MyInt:
	def __init__( self, value: int ) -> None: ...
	
	@classmethod
	def fromhex(cls, s: str) -> Self:
		return cls(int(s, 16))

Also reproduced here:
https://mypy-play.net/?mypy=latest&python=3.11&gist=234463d18e41111a70ac5276325334f7

Expected Behavior

Self should be a placeholder for TypeVar(...)

Actual Behavior

self_test.py:4: error: Variable "typing.Self" is not valid as a type [valid-type]
self_test.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
self_test.py:14: error: Variable "typing.Self" is not valid as a type [valid-type]
self_test.py:14: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

Your Environment

mypy --python-version 3.11 --strict --implicit-reexport --disallow-untyped-defs

Windows 10 Pro 21H2
Python 3.11.0 (main, Oct 24 2022, 18:13:38) [MSC v.1933 32 bit (Intel)] on win32
accept-types==0.4.1
aiofiles==22.1.0
aiohttp==3.8.3
aioshutil==1.2
aiosignal==1.2.0
altgraph==0.17.3
async-timeout==4.0.2
attrs==22.1.0
bbcode==1.1.0
bcrypt==4.0.1
boto3==1.26.8
botocore==1.29.8
cachelib==0.9.0
certifi==2022.9.24
cffi==1.15.1
chardet==5.0.0
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.6
coverage==6.5.0
cryptography==38.0.1
dnspython==2.2.1
et-xmlfile==1.1.0
Flask==2.2.2
Flask-Login==0.6.2
Flask-Session==0.4.0
fpdf==1.7.2
frozenlist==1.3.1
future==0.18.2
gevent==22.10.2
greenlet==2.0.0
idna==3.4
install==1.3.5
itsdangerous==2.1.2
Jinja2==3.1.2
jmespath==1.0.1
lark==1.1.4
MarkupSafe==2.1.1
multidict==6.0.2
mypy==0.991
mypy-extensions==0.4.3
names==0.3.0
natural==0.2.0
netaddr==0.8.0
openpyxl==3.0.10
paramiko==2.11.0
pefile==2022.5.30
Pillow==9.2.0
psutil==5.9.3
psycopg2 @ file:///C:/users/royce3/Downloads/psycopg2-2.9.3-cp311-cp311-win32.whl
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pycryptodomex==3.15.0
pydub==0.25.1
pyinstaller==5.6
pyinstaller-hooks-contrib==2022.10
pyinstrument==4.3.0
PyMySQL==1.0.2
PyNaCl==1.5.0
pyodbc==4.0.34
pyOpenSSL==22.1.0
python-dateutil==2.8.2
python-ldap @ file:///C:/users/royce3/Downloads/python_ldap-3.4.0-cp311-cp311-win32.whl
python-pam==2.0.2
pytz==2022.6
pytz-deprecation-shim==0.1.0.post0
pyvmomi==7.0.3
pywin32==304
pywin32-ctypes==0.2.0
pyzipper==0.3.6
reportlab==3.6.11
requests==2.28.1
s3transfer==0.6.0
service-identity==21.1.0
six==1.16.0
termcolor==2.0.1
tornado==6.2
tornroutes==0.5.1
types-aiofiles==22.1.0.3
types-certifi==2021.10.8.3
types-click==7.1.8
types-cryptography==3.3.23.1
types-Flask==1.1.6
types-Jinja2==2.11.9
types-MarkupSafe==1.1.10
types-paramiko==2.11.6
types-PyMySQL==1.0.19.1
types-pyOpenSSL==22.1.0.2
types-python-dateutil==2.8.19.2
types-pytz==2022.5.0.0
types-pyvmomi==7.0.8.2
types-requests==2.28.11.2
types-setuptools==65.5.0.2
types-six==1.16.21.1
types-termcolor==1.1.6
types-tornado==5.1.1
types-tzlocal==4.2.2.2
types-urllib3==1.26.25.1
types-Werkzeug==1.0.9
typing_extensions==4.4.0
tzdata==2022.5
tzlocal==4.2
urllib3==1.26.12
Werkzeug==2.2.2
wsman==0.9.27
XlsxWriter==3.0.3
yarl==1.8.1
zope.event==4.5.0
zope.interface==5.5.0

@remdragon remdragon added the bug mypy got something wrong label Nov 23, 2022
@AlexWaygood
Copy link
Member

PEP 673 isn't supported by mypy yet, but will be supported in the next release.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2022
@AlexWaygood
Copy link
Member

See #14041. In the meantime, you can use a TypeVar:

from typing import TypeVar

Self = TypeVar("Self")

class MyLock:
	def __enter__(self: Self) -> Self:
		self.lock()
		return self
	
	def lock( self ) -> None: ...

class MyInt:
	def __init__( self, value: int ) -> None: ...
	
	@classmethod
	def fromhex(cls: type[Self], s: str) -> Self:
		return cls(int(s, 16))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants