Skip to content

Make singledispatch only evaluate annotation for dispatched arg #110373

Open
@hauntsaninja

Description

@hauntsaninja

In the example below, we have no real need to evaluate the type annotations of the non-dispatched args.

Skipping this might improve performance / import time a little.
While a little late for some nice features like X | Y syntax, this is useful in case we introduce new typing syntax in the future, since it allows users to use it on non-dispatched args.

from __future__ import annotations
from functools import singledispatch

@singledispatch
def fun(arg, verbose):
    print("arg", type(arg))

@fun.register
def _(arg: int, verbose: 1/0):
    print("arg", type(arg))

@fun.register
def _(arg: list, verbose: 1/0):
    print("arg", type(arg))

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions