python / python/typing

Typing decorators which enrich a class

Open
#1,321 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

Hi

I would like to type a decorator which takes as input a class and returns the same class with added attributes, i.e., a subclass.
Example:

import typing

_T = typing.TypeVar("_T")


def config(*, /, prefix: str) -> typing.Callable[[typing.Type[_T], ???]:
    def wrap(cls: typing.Type[_T]) -> ???:
        class Subclass(cls):  # or could do cls.prefix = prefix instead of subclassing
            prefix: str = prefix

        return Subclass

    return wrap


@config(prefix="test")
class A:
    ...


print(A.prefix)

Been searching for an answer and experimenting for a few days now and can't find anything concrete. Any help would be much appreciated

Thank you

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named. Start by reviewing the current guidance for typing class decorators and the behavior of type checkers for the shown config and wrap signatures; done should mean there is a documented, accepted way to preserve the input class type while exposing the added prefix attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.