Replies: 5 comments
-
|
Have also raised with the official python typing repo: python/typing#1495 |
Beta Was this translation helpful? Give feedback.
-
|
I am encountering a similar scenario and I would really like a solution to this as well. I have a wrapper class around some data querying and parsing methods. However, it is at this moment quite verbose to invoke. current code: # <main.py>
from .lib import fetcher
from .lib import QueryParams
result = fetcher(
query_params = QueryParams(
chicken = 'good',
durian = 'bad',
),
logger = logger,
)
#<lib.py>
class QueryParams: ...
class ParsedData: ...
def queryer(query_params: QueryParams, logger: Logger) -> Response:
...
def parser(response: Response, logger: Logger) -> ParsedData:
...
class Fetcher(Generic[QueryParams, ParsedData]):
def __call__(self, *, query_params: QueryParams, logger: Logger) -> ParsedData:
...
fetcher = Fetcher(queryer, parser)Ideally, it should be invoked like this (with type hinting for fetcher): # <main.py>
from .lib import fetcher
result = fetcher(
chicken = 'good',
durian = 'bad',
logger = logger,
)
#<lib.py>
class QueryParams: ...
class ParsedData: ...
def queryer(query_params: QueryParams, logger: Logger) -> Response:
...
def parser(response: Response, logger: Logger) -> ParsedData:
...
class Fetcher(Generic[QueryParams, ParsedData]):
def __call__(self, *, logger: Logger, **kwargs: Unpack[QueryParams]) -> ParsedData:
query_params = QueryParams(**kwargs)
...
fetcher = Fetcher(queryer, parser)Can anybody suggest a good way to implement this? |
Beta Was this translation helpful? Give feedback.
-
|
After reading many comments and ongoing work, I think the |
Beta Was this translation helpful? Give feedback.
-
|
I've ran into a similar issue and I don't think it's possible right now due to this: https://peps.python.org/pep-0612/#concatenating-keyword-parameters. From my understanding it's not possible to 'reshuffle' kwargs. You can however prepend positional args and get type checking (different use case, but could be insightful): |
Beta Was this translation helpful? Give feedback.
-
|
Here is one way to do it: from typing import Any, Callable, ParamSpec, TypeVar
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
P = ParamSpec('P')
R = TypeVar('R')
def factory(cls: Callable[P, R], /) -> Callable[[Callable[P, R]], Callable[P, R]]:
def inner(func):
return func
return inner
@factory(Person)
def person_factory(**kwargs: Any) -> Person:
return Person(**kwargs)
reveal_type(person_factory) # (*, name: str, age: int) -> Person |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In my open-source library, we've swapped over to use Pydantic for our dataclasses. However, I really want to preserve type hint info, so a lot of our parent methods look like:
When I think it would be more elegant to have something like:
Where the parent then makes the Config obejct itself. But if we implement this right now, the user looses all the type hint info as to what they can pass in, unless we manually duplicate docstring and all the fields from Config, which isn't ideal. I'm trying to see if there's anything we can do it use python 3.12's Unpack kwarg type hint.
Here's a tiny example to show what I mean.
Obviously this doesn't work. TypeDict != dataclass. But I figured, does anyone have any ideas/tricks/something else that might make this factory method type hinting work properly?
Beta Was this translation helpful? Give feedback.
All reactions