Skip to content

Add types.Self #90827

Closed
Closed

Description

@rhettinger
BPO 46669
Nosy @gvanrossum, @rhettinger, @JelleZijlstra
Superseder
  • bpo-46534: Implementing PEP 673 (Self type)
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-02-07.18:09:47.676>
    created_at = <Date 2022-02-06.23:12:51.840>
    labels = ['3.11']
    title = 'Add types.Self'
    updated_at = <Date 2022-02-08.05:25:38.097>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2022-02-08.05:25:38.097>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-02-07.18:09:47.676>
    closer = 'rhettinger'
    components = []
    creation = <Date 2022-02-06.23:12:51.840>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46669
    keywords = []
    message_count = 5.0
    messages = ['412682', '412684', '412772', '412786', '412813']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'rhettinger', 'JelleZijlstra']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '46534'
    type = None
    url = 'https://bugs.python.org/issue46669'
    versions = ['Python 3.11']

    Activity

    1. rhettinger commented on Feb 6, 2022

      @rhettinger
      ContributorAuthor

      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.

    2. JelleZijlstra commented on Feb 6, 2022

      @JelleZijlstra
      Member

      PEP-673 (which was accepted) adds typing.Self already. bpo-46534 tracks implementing it.

    3. rhettinger commented on Feb 7, 2022

      @rhettinger
      ContributorAuthor

      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:
    4. JelleZijlstra commented on Feb 7, 2022

      @JelleZijlstra
      Member

      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 doesn't capture:

      1. The arguments are either all None, or none of them are
      2. If they are not None, then the typ argument is the type of the exception passed to the exc argument

      I believe these invariants are always true in 3.11 thanks to Irit's work, but previously there could be rare circumstances where they didn't hold. You probably know the details better than I do.

      We could support the first invariant by using two overloads, one where the arguments are all None and one where they aren't. I don't think that's generally worth doing though: it's a lot of verbose code and doesn't fix many real problems.

      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.

    5. rhettinger commented on Feb 8, 2022

      @rhettinger
      ContributorAuthor

      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 invariant correctly?

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixes

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions