Skip to content

Use built-in Self type in __init__.pyi when available#114

Merged
Marco-Sulla merged 1 commit intoMarco-Sulla:masterfrom
adriengcql:patch-2
Jan 16, 2026
Merged

Use built-in Self type in __init__.pyi when available#114
Marco-Sulla merged 1 commit intoMarco-Sulla:masterfrom
adriengcql:patch-2

Conversation

@adriengcql
Copy link
Contributor

@adriengcql adriengcql commented Jan 13, 2026

Starting in python 3.11 the typing module has a Self type that does the equivalent of the SelfT typevar.

This integrate a bit better with type checkers.
Seen with ty in class inheritance (it is essentially a problem in ty but is a sounds like a good occasion to make the update):

from frozendict import frozendict

class MyDict(frozendict[int, int]):
    pass


a = MyDict()
b = a.set(1, 1)
test.py:8:5: error[no-matching-overload] No overload of function `__new__` matches arguments
test.py:9:5: error[no-matching-overload] No overload of bound method `set` matches arguments

@Marco-Sulla
Copy link
Owner

Marco-Sulla commented Jan 13, 2026

Failed for Python 3.8 - 3.10:

  typed.py:24: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]
  typed.py:25: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]
  typed.py:40: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]
  typed.py:42: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]
  typed.py:47: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]
  typed.py:56: error: Expression is of type "Any", not "frozendict[str, int]"  [assert-type]

Notice that it doesn't fail on every archs because type checking is only enabled for the primary workflow. Furthermore it doesn't fail on 3.6 - 3.7 because typing_extension is only available for 3.8+.

It's quite strange it doesn't work. Maybe it's a stupid idea, but try moving the import at the same level of the other imports, just before K = TypeVar("K"). It's also desired for a matter of style.

Starting in python 3.11 the typing module has a Self type that does the equivalent of the SelfT typevar.

This integrate a bit better with type checkers.
@adriengcql
Copy link
Contributor Author

Thanks for the insight, it seems not to work well with ImportError so I switched to a condition on the python version.

@Marco-Sulla Marco-Sulla merged commit c4507e4 into Marco-Sulla:master Jan 16, 2026
53 checks passed
@Marco-Sulla
Copy link
Owner

It's odd. It works for TypeVar, for example, and not for Self. Mah. Typing in Python gives me an headache every time. There should be a more obvious way to do typing in py, but I'm not Dutch.

@Marco-Sulla
Copy link
Owner

Anyway, ty for the improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants