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

chore: Embrace the walrus operator #24127

Merged
merged 1 commit into from
May 19, 2023

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented May 19, 2023

SUMMARY

In #23890 (thanks to @sebastianliebscher) we dropped support for Python 3.8 and thus now we are free to embrace the walrus (:=) operator. This PR adds a pre-commit hook (which runs before black) to ensure we never have code of the form,

 n = 10
 if n > 3:
     print(n)

but rather,

if (n := 10) > 3:
    print(n)

Note the pre-commit hook only detects/remedies simple use cases. Personally I've found the walrus operator really handy in list comprehensions (when you need to assign a temporary variable) and logic like,

foo = bar()
return foo.value if foo else None

which becomes,

return foo.value if (foo := bar()) else None

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@john-bodley john-bodley requested a review from a team as a code owner May 19, 2023 00:15
@codecov
Copy link

codecov bot commented May 19, 2023

Codecov Report

Merging #24127 (f15554e) into master (2222073) will decrease coverage by 0.04%.
The diff coverage is 64.40%.

❗ Current head f15554e differs from pull request most recent head ed4e4c8. Consider uploading reports for the commit ed4e4c8 to get more accurate results

@@            Coverage Diff             @@
##           master   #24127      +/-   ##
==========================================
- Coverage   68.30%   68.26%   -0.04%     
==========================================
  Files        1952     1952              
  Lines       75432    75388      -44     
  Branches     8191     8202      +11     
==========================================
- Hits        51521    51462      -59     
- Misses      21807    21819      +12     
- Partials     2104     2107       +3     
Flag Coverage Δ
hive 53.23% <23.75%> (+0.06%) ⬆️
mysql 78.94% <80.00%> (-0.01%) ⬇️
postgres 79.01% <80.00%> (-0.01%) ⬇️
presto 53.16% <25.00%> (+0.06%) ⬆️
python 82.81% <83.75%> (-0.01%) ⬇️
sqlite 77.54% <77.50%> (+<0.01%) ⬆️
unit 53.18% <12.50%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...set-ui-core/src/ui-overrides/ExtensionsRegistry.ts 100.00% <ø> (ø)
...et-frontend/src/components/TableSelector/index.tsx 81.08% <ø> (ø)
...end/src/features/databases/DatabaseModal/styles.ts 77.66% <ø> (ø)
superset-frontend/src/pages/DatabaseList/index.tsx 66.03% <0.00%> (ø)
superset/datasets/commands/importers/v1/utils.py 79.41% <0.00%> (+0.77%) ⬆️
superset/db_engine_specs/databricks.py 70.92% <0.00%> (+0.49%) ⬆️
superset/db_engine_specs/snowflake.py 67.64% <0.00%> (+0.39%) ⬆️
superset/migrations/env.py 0.00% <0.00%> (ø)
superset/models/helpers.py 69.90% <0.00%> (+0.07%) ⬆️
superset/views/api.py 68.75% <0.00%> (+1.05%) ⬆️
... and 45 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

datasource = form_data.get("datasource", "")

if "__" in datasource:
# pylint: disable=superfluous-parens
Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly we're stuck between a rock and a hard place here. There is a Pylint false positive on line #265 but if we add the disable comment there then Black will reformat the line because its > 88 characters and the formatting then requires the extra (...) per Pylint's rules.

@@ -19,6 +19,10 @@ repos:
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/MarcoGorelli/auto-walrus
Copy link
Member Author

Choose a reason for hiding this comment

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

Probably should reformat the code before running Mypy, Black, etc.

@villebro
Copy link
Member

villebro commented May 19, 2023

Lol, love the auto walrus! Btw walrus was already added in 3.8, so it's been supported on the codebase for quite some time (3.7 was deprecated here in March 2022: #19017)

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

I browsed through all the changes, and they LGTM. Happy to see this enforced, as I'm also a big fan of the walrus.

@john-bodley john-bodley merged commit d583ca9 into apache:master May 19, 2023
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.0.0 labels Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 3.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants