Replies: 1 comment 2 replies
-
I understand the problem you're trying to solve, but this solution won't work. First, a solution of this sort would need to go through a spec'ing and standardization process. It's not something that I'd implement unilaterally in pyright without it becoming part of the type system. Second, there are many problems with the proposed solution.
If you would like to discuss this problem and solicit ideas for solutions, I recommend opening an issue in the python/typing discussion forum or the python.org typing discourse forum. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would love for pyright (and all type checkers) to be able to infer the fields on a dynamically-generated dataclass type, based on its statically-typed
__init__
signature. E.g.As you well know, PEP 681 enables you to statically declare fields on a class to inform the
__init__
of that class in the eyes of type checkers. What I am proposing is enabling this to go in the opposite direction, so to speak.Motivation
Many experiment / configuration framework libraries (including hydra-zen) have gravitated towards using dataclass types to represent "configs" for functions. The following pattern is quite common:
Config
is acting like a floating, mutable signature forfunc
. Its defaults can be modified/overwritten via inheritance, and you can create and store many instances ofConfig
as a way to callfunc
in various ways later. These are the upsides to this approach. The downsides, however, are numerous - such as decouplingConfig
from the implementationfunc
that you care about, and no longer being able to callfunc
in an ergonomic way. Soon, your code base is littered with this pattern instead of having simple functions.This pattern can be seen throughout nerfstudio and many many ML code bases.
Far preferable would be to leave
func
as a normal function and then generate theConfig
type dynamically, based off of its signature.then it is simple enough to write a wrapper that lets you do
call_via_config(foo)(Config(x=1, y="b"))
to call the function using your config instance. Now, the simple functionfoo
is the star player in your code base once again and your desire to create a type that configure it can be done automatically and as an after thought.Indeed this is the pattern that hydra-zen enables. But a major downside to this dynamic approach is the lack of type-checking/autocomplete support for the fields on the generated type.
I have to imagine that libraries like
pydantic
and other heavy users of PEP 681 would have use for this feature as well.Limitations
In this proposal,
P
is only valid in this context if there are no*args
,**kw
, or positional-only entries in the function's signature - these are not supported by dataclass types.dataclass features not supported:
InitVar
entries in the signatureBeta Was this translation helpful? Give feedback.
All reactions