[issue46669] Add types.Self

2022-02-07 Thread Raymond Hettinger
Raymond Hettinger added the comment: > Your suggested signature looks like it's trying to support > the second invariant, but it doesn't quite: if the types > don't match, the type checker will just set T to the > common base type of the two arguments. Is there a way to write the second

[issue46669] Add types.Self

2022-02-07 Thread Jelle Zijlstra
Jelle Zijlstra added the comment: The conventional way is to write def __exit__( self, __typ: type[BaseException] | None, __exc: BaseException | None, __tb: types.TracebackType | None, ) -> None: ... But there are two invariants about how __exit__ works in practice that this

[issue46669] Add types.Self

2022-02-07 Thread Raymond Hettinger
Change by Raymond Hettinger : -- resolution: -> duplicate stage: -> resolved status: open -> closed superseder: -> Implementing PEP 673 (Self type) ___ Python tracker ___

[issue46669] Add types.Self

2022-02-07 Thread Raymond Hettinger
Raymond Hettinger added the comment: On a related note, is this the correct way to annotate __exit__? Exc = TypeVar('Exc', bound=Exception) def __exit__(self, exctype: Optional[Type[Exc]], excinst: Optional[Exc], exctb: Any) -> None: -- ___

[issue46669] Add types.Self

2022-02-06 Thread Jelle Zijlstra
Jelle Zijlstra added the comment: PEP 673 (which was accepted) adds typing.Self already. bpo-46534 tracks implementing it. -- ___ Python tracker ___

[issue46669] Add types.Self

2022-02-06 Thread Raymond Hettinger
New submission from Raymond Hettinger : Typeshed now has a nice self-describing type variable to annotate context managers: Self = TypeVar('Self') def __enter__(self: Self) -> Self: return self It would be nice to have that in the standard library types module as well. --