-
-
Notifications
You must be signed in to change notification settings - Fork 79
Implement descriptor factory typing system #1066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewing this, I guess it looks fine. The What's the status of this? Are you looking for specific feedback? Does it supersede the previous attempts entirely? What does it take to get it in? |
Thanks for creating this. How do you plan to get a proper init signature? I don't see this in this PR? |
Indeed pretty gnarly, but it is what it is.
Planning to add tests and then start typing each parameter until the tests pass, or I have to loosen the tests if achieving proper typing isn't actually possible. For example I started on ObjectSelector and am dubious that I can correctly type the value as
Not really, unless someone can propose yet another cleaner or better alternative.
Yes!
You mean |
Parameterized init signatures. That was the starting point for my PR analysis. |
Another source of inspiration is mssgspec. It's quite readable. They use @dataclass_transform too to get init right. See https://github.com/jcrist/msgspec/blob/main/msgspec/__init__.pyi. |
Yep also dubious this will be possible. I'd probably be fine with adding an
I'm fine with that being done in a separate PR. So I guess that means this PR doesn't strictly supersede #1065 (though as far as I could see #1065 was not implementing |
#1065 is using @dataclass_transform. Its the whole point that we need an approach that supports both attribute types and init signatures. They need to be compatible. Can't create one feature without at least proof that other feature will be possible. |
Didn't work for me there #1065 (comment). How did you get it to work? Maybe I'm doing something wrong. |
Yeah also tried #1065 and had no luck getting it to work for |
what’s the if it’s there in order to keep Sphinx from picking up all the alternative signatures, I think it might be possible to use this: https://github.com/tox-dev/sphinx-autodoc-typehints/blob/e9db2aaee73c6dfe466cde83ea502dc4c33c2fb6/src/sphinx_autodoc_typehints/patches.py#L20-L35 |
Okay, I have gotten much further along here and though there is still plenty of work to fix runtime behavior I've broken, finish typing all of param and define appropriate overloads for all Parameter types, I now have an understanding of The ProblemOverall class MyClass(Parameterized):
string_param: str = String() the type definition is always needed, even though the class MyClass(Parameterized):
string_param: String[str] = String() And finally, we cannot declare all Parameter types as valid dataclass field specifiers because each An AlternativeOkay, so it's now clear that we will never get automatically generated class MyClass(TypedParameterized):
string: str
explicit_str: str = param(str, doc="Explicitly defined parameter type definition")
explicit_str_param: str = param(param.String, doc="Explicitly defined parameter instance definition")
Optionally we can then explore whether we can leverage and upgrade existing dataclasses and pydantic models by using |
Also I realize much of this just recaps what @MarcSkovMadsen has already laid out above. I just wanted to record all the limitations for myself. As was also indicated previously, this PR will aim to do two things:
Separately we should agree for a new proposal for a |
TypedParameterized -> Paramtyped ? Typedparams? A mixin makes sense. Making a conversion function from one to the other (including as a decorator) would also make sense; not sure of the pros and cons. What about making this new class be a dataclass already? See https://docs.pydantic.dev/latest/concepts/dataclasses/ for how Pydantic handles this. |
I do like the idea of a |
Totally agree, hoping I can wrap this up pretty soon. Once that's done I can begin working on a proposal for a |
Supersedes #1065 and #985
After reviewing the other typing PRs and reading in a bit more depth on what is possible, including traitlets source code (which @maximlt pointed me to), this PR aims to provide an implementation of typing using
Generic
s plus factory style typing overloads that handle polymorphic types that depend on a dynamic value (likeallow_None
).This approach seems to play well with
pylance
so correctly infers types inside VSCode, without having the user have to manually type things, even if multiple type variants exist for a particular Parameter type. I've not gotten very far down the road of typing each parameter type but at least for the String type this works well, i.e. this works: