Type interference: `ProtocolOf`?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reviewing the issue's examples and the referenced typing.get_args, Callable, Protocol, ParamSpec, TypedDict, Unpack, and dataclasses APIs. The issue names no repository files or tests; done would require a focused, agreed design for the proposed typing feature before implementation can be scoped.
Written by the indexing model from the issue text.
Description
I'm looking the way to interfere existing types explicitly, like typeof / keyof in Typescript
I have 2 use cases:
- existing function definition ->
Callable[...] - dataclass
__init__signature / protocol
I think it should look like:
from typing import ProtocolOf
def foo(a: int, b: float) -> float:
return a * b
FooProtocol = ProtocolOf[foo]
Should be equal to:
class FooProtocol(Protocol):
def __call__(a: int, b: float) -> float: ...
Or dataclasses:
from dataclasses import dataclass
from typing import ProtocolOf
@dataclass()
class Foo:
a: int
b: float
Here I'm not sure which way is better
# way 1:
FooProtocol = ProtocolOf[Foo]
Should be equal to:
class FooProtocol(Protocol):
def __call__(a: int, b: float) -> Foo: ...
# way 2:
FooInitProtocol = ProtocolOf["Foo.__init__"]
Should be equal to:
class FooInitProtocol(Protocol):
def __call__(a: int, b: float) -> None: ...
Callable / Protocol to ParamSpec / Returning to use in typing
I see typing.get_args but it doesn't looks suitable for typing. Also
from collections.abc import Callable
from typing import get_args
FooSignature = Callable[[int, float], float]
get_args(FooSignature) # == [[int, float], float]
Instead of that it should be way to define FooParamSpec / FooReturning
Bonus
there are no way to convert TypedDict to ParamSpec.kwargs
UPD: is't bound to typing.Unpack
- 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 ·