-
Notifications
You must be signed in to change notification settings - Fork 18
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
♻️ Don't use None as default for required arguments #466
Comments
When talking to @joernweissenborn he explained that the idea behind A quick reminder on how 'normal' arguments work in python. They allow both the usage as keyword arguments AND positional arguments. In [1]: def f(x, y=1):
: return x + y
:
In [2]: f(2)
Out[2]: 3
In [3]: f(x=2)
Out[3]: 3
In [4]: f(y=3, x=2)
Out[4]: 5
In [5]: f(1, 2)
Out[5]: 3 To force users to only use keyword arguments, for better readability (more verbose and unambiguous) e.g. for the In [7]: g(2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-3580407364ae> in <module>
----> 1 g(2)
TypeError: g() takes 0 positional arguments but 1 was given
In [8]: g(y=3, x=2)
Out[8]: 5 If we want users to only use positional arguments (the only reasonable use case, being not yet final argument names) we could use positional only arguments, since our new min version of python is 3.8 In [2]: def h(x, y=1, /):
...: return x + y
...:
In [3]: h(x=2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-ad2c09c39c80> in <module>
----> 1 h(x=2)
TypeError: h() got some positional-only arguments passed as keyword arguments: 'x'
In [4]: h(2)
Out[4]: 3
In [5]: h(2, 3)
Out[5]: 5 Please note that those can also be mixed. Here is also a nice video explaining how they work (just as a reference) 😉 |
Just a Note (rev: f366ed6): class DummyAttr:
_glotaran_has_label = False
@model(
"foo",
attributes={},
megacomplex_type=DummyAttr,
matrix=lambda: None, # type:ignore
model_dimension="",
global_dimension="",
)
class FooModel:
pass pyglotaran/glotaran/model/decorator.py Lines 78 to 95 in f366ed6
|
I think a lot of improvement toward this issue were incorporated in #1135, perhaps time for another review. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to lack of recent activity. Please open a new issue if you're still encountering this problem. Thanks for your contributions! |
Description
While writing type stub files for the runtime patched objects (Models and Modelattributes) due to known limitations of mypy. I found that some functions have default value of
None
for arguments that are required. This suggests that you could use them without or fewer than required arguments.Also, to make sense of the tracebacks the user needs detailed knowledge of the glotaran internals e.g. the model decorator.
What I Did
The signature of the model decorator
pyglotaran/glotaran/model/decorator.py
Lines 59 to 76 in 0a3f5e2
suggests that you could use it like this to generate the most minimal (even so nonsensical) Model
While the most minimal way for it not to crash is
Another instance where this is the case is in
kinetic_image_matrix
pyglotaran/glotaran/builtin/models/kinetic_image/kinetic_image_matrix.py
Lines 11 to 19 in 0a3f5e2
Where nearly all arguments are required but defaulted to
None
.The text was updated successfully, but these errors were encountered: