-
-
Notifications
You must be signed in to change notification settings - Fork 689
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
Constructor argument cleanups #3134
Comments
I agree that we should massively discourage the use of positional arguments. If nothing else, it will make our migration path easier, as we don't have to worry about parsing specific argument orders. However I'm not completely sure I agree with what you're proposing (at least, as I understand it). For example - it sounds like you're proposing that Label's constructor (minus type annotations) should literally be:
That's unarguably more generic, but means a loss of detail for any code inspection tools in your IDE (including any type annotation-based checks), and requires that we duplicate a bunch of argument handling logic that Python gives us for free. The benefit you mention of duplication properties is also predicated on the properties being 1-1 matches for their constructor arguments... and I'm not 100% sure that's actually the case (I could be wrong, but it's setting off some bells in my mind). At the very least, there's a couple of places where we have "only available during construction" arguments. I'd argue that:
would be a better end state - that is:
That means we get all the built-in argument handling and type annotation checking that Python provides; but enforces the use of keyword arguments. |
That's a good point. In that case, let's take (text=None, icon=None, id=None, style=None, on_press=None, enabled=True, **kwargs) The transitional signature would be: (text=None, *args, icon=None, id=None, style=None, on_press=None, enabled=True, **kwargs) In the transitional phase, the first thing each constructor would do is call a helper function to extract any deprecated positional arguments from icon, id, style, on_press, enabled = parse_positional(
args, icon=icon, id=id, style=style, on_press=on_press, enabled=enabled
) After the deprecation period, the final signature would be: (text=None, *, icon=None, id=None, style=None, on_press=None, enabled=True, **kwargs)
I think even in those cases there's usually a read-only property with the same name. For example, Anyway, if we change the signature as laid out above, then the duplicate documentation would be an independent issue which could be fixed before, after, or not at all. In most cases, I think it would be clearer for the constructor documentation to cover the initialization-specific details, if any, and then finish with a sentence like "All other arguments are assigned to the properties with the same names." |
This makes sense to me as a transition plan and final state.
I agree that at the conceptual level, there's an opportunity for de-duplication of documentation here; I have 2 concerns about documenting the arguments on the constructor as "see properties".
However, I also agree that this could be tackled as a follow-up concern. |
Widget constructor arguments follow a fairly consistent order:
Label(text)
andImageView(image)
id
style
Currently all arguments can be passed either with positional or keyword syntax. However, using positional syntax for anything except the required argument is not very clear. So I'd like to propose that we deprecate passing all other arguments positionally.
To detect how an argument was passed, all constructors would need to have a signature of the form:
They could then parse the
args
andkwargs
by passing them to a helper function along with a tuple specifying the deprecated positional order. After a reasonable deprecation period, theargs
part of the signature would be removed.The docstring of
kwargs
would change from "initial style properties" to "initial properties", thus making explicit the practice that we've long followed, that every writable property has a constructor argument.This means the constructor signatures would no longer list all their arguments individually. But I actually think that would be an improvement, as the constructor documentation mostly duplicates the property documentation anyway. The only additional information it contains is the default values, and they can be moved to the documentation of the individual properties.
So far I've only mentioned widgets, but this pattern could apply to other classes as well.
The text was updated successfully, but these errors were encountered: