-
-
Notifications
You must be signed in to change notification settings - Fork 815
Eliminates B006 warnings from root-cause #4826
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR removes mutable default arguments (B006 warnings) by changing list/dict defaults to None
and initializing them inside constructors or subclass hooks.
- Replace mutable defaults (lists/dicts) with
Optional[...] = None
in multiple element constructors - Add
if ... is None:
checks to lazily initialize defaults - Update
__init_subclass__
inelement.py
to apply the same pattern for class-level lists
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
nicegui/elements/scene.py | Made click_events optional and added lazy initialization |
nicegui/elements/markdown.py | Made extras optional and added lazy initialization |
nicegui/elements/leaflet.py | Made options optional and added lazy initialization |
nicegui/elements/keyboard.py | Made ignore optional and added lazy initialization |
nicegui/elements/interactive_image.py | Made events optional and added lazy initialization |
nicegui/elements/aggrid.py | Made html_columns and options optional and added init logic |
nicegui/element.py | Changed class-level list defaults to optional and fallback init |
Comments suppressed due to low confidence (4)
nicegui/element.py:99
- [nitpick] Using
dependencies = dependencies or []
may unintentionally replace an explicit empty list; considerif dependencies is None: dependencies = []
for clearer intent and to avoid edge cases with falsy values.
dependencies = dependencies or []
nicegui/elements/aggrid.py:59
- The
Dict
type is unparameterized here, which limits type safety; consider usingDict[str, Any]
(or a more specific signature) to improve clarity and static analysis.
options: Optional[Dict] = None
nicegui/elements/leaflet.py:30
- Similar to other parts of the codebase, this
Dict
could be parameterized (e.g.Dict[str, Any]
) to provide better type hints and prevent misuse at call sites.
options: Optional[Dict] = None,
nicegui/element.py:100
- [nitpick] For consistency and clarity, use an explicit
if libraries is None:
check instead oflibraries or []
to initialize the default list.
libraries = libraries or []
@@ -13,7 +13,7 @@ class Markdown(ContentElement, component='markdown.js', default_classes='nicegui | |||
|
|||
def __init__(self, | |||
content: str = '', *, | |||
extras: List[str] = ['fenced-code-blocks', 'tables'], # noqa: B006 | |||
extras: Optional[List[str]] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring still describes the default extras
as ['fenced-code-blocks', 'tables']
; consider updating it to note that passing None
results in those defaults being applied internally.
Copilot uses AI. Check for mistakes.
In my point of view this is not a breaking change because we're loosening the type constraint. Since In my opinion, using And |
Interesting. How about |
Also, if I understand correctly, since tuples can alo be |
Good question. Actually, one reason why we kept using Another aspect I haven't though of: If someone derived, e.g.,
That's the point of the whole exercise. Our code should never mutate such input parameters because their default value is "static". If one As far as I can tell, within the NiceGUI library we already never mutate default arguments. But user code could - accidentally or intentionally - break this convention, allowing for nasty bugs down the line. By changing the signatures to immutable types, we enforce derived classes to follow this rule as well. |
Motivation
Addresses NiceGUI Wishlist
Implementation
click_events: List[str] = ('click', 'dblclick')
, but that would cause mypy to complain.click_events: Sequence[str] = ('click', 'dblclick')
could be doable, but we are changingList
toSequence
, not sure if that'd be a breaking changeclick_events: Optional[List[str]] = None
withif click_events is None:
click_events = ['click', 'dblclick']
Progress
Tests are green at https://github.com/evnchn/nicegui/actions/runs/15380276062, but the point about whether this is a breaking change is still worthy.