Skip to content
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

Remove mutable default args in function definitions #488

Closed
mpkocher opened this issue Jun 22, 2019 · 5 comments
Closed

Remove mutable default args in function definitions #488

mpkocher opened this issue Jun 22, 2019 · 5 comments
Labels
Milestone

Comments

@mpkocher
Copy link

There appears to be several cases where mutable args, such as a list or dict, are used as function defaults.

https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

pylint panel | grep  dangerous-default-value
panel/viewable.py:406:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/util.py:118:0: W0102: Dangerous default value [] as argument (dangerous-default-value)
panel/param.py:345:12: W0102: Dangerous default value [] as argument (dangerous-default-value)
panel/pipeline.py:32:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
panel/interact.py:126:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/interact.py:382:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/holoviews.py:176:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:83:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:123:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:170:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:178:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:186:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:244:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/pane/vtk/vtkjs_serializer.py:291:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/io/save.py:48:0: W0102: Dangerous default value {} as argument (dangerous-default-value)
panel/io/model.py:56:0: W0102: Dangerous default value [] as argument (dangerous-default-value)

Also, I believe it would be useful to add an autoformatting tool such as black or autopep8 to the commit/PR process when time permits.

@jbednar
Copy link
Member

jbednar commented Jun 22, 2019

Thanks. I don't have any opinion pro or con about autoformatting; if it worked well with Parameter declarations then I'd be ok with it. Up to @philippjfr.

From what I can tell, most of those warnings could be avoided by replacing a declaration like def fn(x={}) with def fn(x=None); if x is None x={}, and that seems like a good idea. But at least two of them appear to me to be a very deliberate usage of the mutable argument to collect state across multiple calls, as noted below:

panel/viewable.py:406:4: Could replace with None
panel/util.py:118:0: Used deliberately to collect arguments in a recursive call
panel/param.py:345:12: Used deliberately to collect arguments in a recursive call
panel/pipeline.py:32:4: Could replace with None
panel/interact.py:126:4: Could replace with None
panel/interact.py:382:4: Could replace with None
panel/pane/holoviews.py:176:4: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:83:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:123:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:170:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:178:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:186:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:244:0: Could replace with None
panel/pane/vtk/vtkjs_serializer.py:291:0: Could replace with None
panel/io/save.py:48:0: Appears to be unused; could replace with None
panel/io/model.py:56:0: Could replace with None

If someone else could double-check my categorizations, then I'd be happy to see a PR where the two deliberate usages are marked with #noqa and the rest are replaced with None.

@philippjfr
Copy link
Member

philippjfr commented Jun 24, 2019

Thanks for the report. It would be nice to get rid of most of these. I tried to avoid the pattern of caching stuff in default kwargs but in certain cases it seems those stuck around. I don't think it's a huge priority to fix those instances so removing the rest and putting a # noqa around them seems reasonable to me.

I do like black but it doesn't play that nicely with param so I don't think we could adopt it. I haven't played with autopep8 to see how well that works but it's certainly worth investigating.

@philippjfr philippjfr added the good first issue Good for newcomers label Jul 25, 2019
@philippjfr philippjfr added this to the v0.7.0 milestone Jul 25, 2019
@xtaje
Copy link
Contributor

xtaje commented Oct 12, 2019

Will submit as several PRS per package. First one is #692.

@xtaje
Copy link
Contributor

xtaje commented Oct 12, 2019

#692 and #694 should fix the items listed above, except for panel/param.py, which did looked non-trivial to verify by inspection.

@philippjfr
Copy link
Member

Thanks for helping out @xtaje

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants