Skip to content

refactor: Simplify SchemaBase.copy#3543

Merged
binste merged 4 commits intovega:mainfrom
dangotbanned:simplify-schemabase-copy
Aug 18, 2024
Merged

refactor: Simplify SchemaBase.copy#3543
binste merged 4 commits intovega:mainfrom
dangotbanned:simplify-schemabase-copy

Conversation

@dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Aug 18, 2024

Related

Description

This PR addresses one complex-structure violation, adds some typing, and gives a little performance boost in SchemaBase.copy.

See each commit message for more detail on specific changes.

Last commit message (auto-added)

  • Initialize an empty set once at origin, rather than creating a new list per iteration
    • Renamed to by_ref in the new private function, to better describe the operation.
    • No change to public API.
  • Define a partial copy to reduce repetition
  • Use a genexpr for args, to avoid unpacking twice

The nested functions do not reference `self` and can be split out, much like `.to_dict` -> `_todict`.
Saw this as a chance to add annotations as well. I think this helps illustrate that `.copy` is a noop for anything other than `SchemaBase | dict | list` - which I think might need to be addressed in the future.
10 lines shorter and is no longer constrained to `list` on the assert.
May be slightly faster on `python<3.11` which do not have zero-cost exceptions https://docs.python.org/3.11/whatsnew/3.11.html#misc
Remembered a related `ruff` rule [FURB145](https://docs.astral.sh/ruff/rules/slice-copy/).
Wouldn't have made this fix, but likely would have led someone there
- Initialize an empty set **once** at origin, rather than creating a new list per iteration
  - Renamed to `by_ref` in the new private function, to better describe the operation.
  - No change to public API.
- Define a partial `copy` to reduce repetition
- Use a genexpr for `args`, to avoid unpacking twice
@dangotbanned dangotbanned marked this pull request as ready for review August 18, 2024 13:44
Copy link
Contributor

@binste binste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @dangotbanned!

@binste binste merged commit c9f9438 into vega:main Aug 18, 2024
@dangotbanned
Copy link
Member Author

LGTM, thanks @dangotbanned!

Thanks @binste

@dangotbanned dangotbanned deleted the simplify-schemabase-copy branch August 18, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments