GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add typing.override decorator · Issue #101561 · python/cpython

Add typing.override decorator #101561

New issue
Copy link
New issue
Copy link
Closed
Closed
Add typing.override decorator#101561
Copy link
Labels
3.12only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@stroxler
stroxler
opened on Feb 4, 2023
Issue body actions

Feature or enhancement

See PEP 698 for details.

The typing.override decorator should, at runtime, attempt to set the __override__ attribute on its argument to True and then return the argument. If it cannot set the __override__ flag it should return its argument unchanged.

Pitch

The purpose of typing.override is to inform static type checkers that we expect this method to override some attribute of an ancestor class. By having this decorator in place, a developer can ensure that static type checkers will warn them if a base class method name changes.

To quote the PEP consider a code change where we rename a parent class method.

The original code looks like this:

class Parent:
    def foo(self, x: int) -> int:
        return x

class Child(Parent):
    def foo(self, x: int) -> int:
        return x + 1

def parent_callsite(parent: Parent) -> None:
    parent.foo(1)

def child_callsite(child: Child) -> None:
    child.foo(1)

And after our rename it looks like this:

class Parent:
    # Rename this method
    def new_foo(self, x: int) -> int:
        return x

class Child(Parent):
    # This (unchanged) method used to override `foo` but is unrelated to `new_foo`
    def foo(self, x: int) -> int:
        return x + 1

def parent_callsite(parent: Parent) -> None:
    # If we pass a Child instance we’ll now run Parent.new_foo - likely a bug
    parent.new_foo(1)

def child_callsite(child: Child) -> None:
    # We probably wanted to invoke new_foo here. Instead, we forked the method
    child.foo(1)

In the code snippet above, renaming foo to new_foo in Parent invalidated the override foo of Child. But type checkers have no way of knowing this, because they only see a snapshot of the code.

If we mark Child.foo as an override, then static type checkers will catch the mistake when we rename only Parent.foo:

from typing import override

class Parent:
    def new_foo(self) -> int:
        return 1

    def bar(self, x: str) -> str:
        return x

class Child(Parent):
    @override
    def foo(self) -> int:   # Type Error: foo does not override an attribute of any ancestor
        return 2

Previous discussion

PEP 698 has details about the proposal itself.

Discussion on this proposal has happened on typing-sig and on [discuss.python.org)[https://discuss.python.org/t/pep-698-a-typing-override-decorator/20839].

Linked PRs

  • gh-101561: Add typing.override decorator #101564
Reacted by qzhang
Reacted by Alex Waygood and Younghoon YUN
Reacted by qzhang

Activity

  1. stroxler
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Feb 4, 2023
  2. JelleZijlstra
    added
    topic-typing
    on Feb 4, 2023
  3. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.12only security fixesonly security fixes
    on Feb 4, 2023
  4. JelleZijlstra commented on Feb 27, 2023

    @JelleZijlstra
    JelleZijlstra
    on Feb 27, 2023
    Member
    More actions

    Merged, thanks @stroxler!

  5. JelleZijlstra
    closed this as completedon Feb 27, 2023
  6. JelleZijlstra
    added a commit that references this issue on Feb 27, 2023

    gh-101561: Add typing.override decorator (#101564)

    0f89acf
  7. carljm
    added a commit that references this issue on Feb 28, 2023

    Merge branch 'main' into inlinecomp2

    73dc0ed
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.12only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.