Merging/Modifying Types
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
Research direction
Start by reviewing the proposed Merge type and the dataclass_transform limitation described in the issue. Determine whether the typing specification has an appropriate place for this behavior and what compatibility and decorator semantics would need to be defined; the issue provides no files, tests, or concrete implementation entry point.
Written by the indexing model from the issue text.
Description
I have a decorator that modifies a class at runtime, kind of like a dataclass:
@modify
class Something:
...
Let's say modify adds foo onto Something, so after calling modify you could use it like so:
@modify
class Something
...
bar = Something.foo()
dataclass_transform doesn't support this kind of behavior at all. So, how would an API like this work?
I don't see why it would be difficult to implement some sort of Merge type, that takes in a type and "applies"" a Protocol to it. For the above example, it would work like this:
T = TypeVar("T")
class FooProtocol(Protocol):
@classmethod
def foo() -> None:
...
def modify(tp: type[T]) -> Merge[T, FooProtocol]:
tp.foo = ... # type: ignore
return tp
@modify
class Something:
...
bar = Something.foo()
In the above example, ideally it would just sort of mark that the T is now compatible with FooProtocol, but a type: ignore is used inside of modify because it would probably be a mess to try and come up with some sort of API for actually converting a T to Merge[T, ...].
TL;DR A type that marks another type as being compatible with a protocol could be handy for making dataclass-like decorators.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
learningequality/ricecooker#747 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
run-llama/llama_index#23199 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
KhronosGroup/glTF-Blender-IO#2769 ·