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

style(mypy): Enforcing typing for superset.translations module #9800

Conversation

john-bodley
Copy link
Member

SUMMARY

Adding mypy type enforcement to the superset.translations module.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TEST PLAN

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

Reviewers

to: @etr2460 @villebro

@john-bodley john-bodley changed the title [mypy] Enforcing typing for translations chore: enforcing mypy typing for translations May 13, 2020
@john-bodley john-bodley changed the title chore: enforcing mypy typing for translations style: enforcing mypy typing for translations May 13, 2020
@codecov-io
Copy link

codecov-io commented May 13, 2020

Codecov Report

Merging #9800 into master will increase coverage by 6.50%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9800      +/-   ##
==========================================
+ Coverage   64.35%   70.85%   +6.50%     
==========================================
  Files         536      588      +52     
  Lines       29106    30472    +1366     
  Branches     2806     3153     +347     
==========================================
+ Hits        18732    21592    +2860     
+ Misses      10194     8767    -1427     
+ Partials      180      113      -67     
Flag Coverage Δ
#cypress 53.65% <ø> (-0.06%) ⬇️
#javascript 59.02% <ø> (?)
#python 71.06% <100.00%> (ø)
Impacted Files Coverage Δ
superset/translations/utils.py 87.50% <100.00%> (ø)
superset-frontend/src/setup/setupPlugins.ts 8.00% <0.00%> (-92.00%) ⬇️
...rset-frontend/src/setup/setupErrorMessagesExtra.ts 50.00% <0.00%> (-50.00%) ⬇️
superset-frontend/src/setup/setupErrorMessages.ts 66.66% <0.00%> (-33.34%) ⬇️
superset-frontend/src/setup/setupApp.ts 25.00% <0.00%> (-3.58%) ⬇️
superset-frontend/src/featureFlags.ts 100.00% <0.00%> (ø)
superset-frontend/src/SqlLab/utils/sqlKeywords.ts 100.00% <0.00%> (ø)
...et-frontend/src/dashboard/util/isDashboardEmpty.ts 100.00% <0.00%> (ø)
...rontend/src/SqlLab/components/QueryAutoRefresh.jsx 65.90% <0.00%> (ø)
...rontend/src/messageToasts/enhancers/withToasts.tsx 100.00% <0.00%> (ø)
... and 186 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf30e16...7486f28. Read the comment docs.

@john-bodley john-bodley force-pushed the john-bodley--mypy-enforcement-translations branch from 7486f28 to 0c8f962 Compare May 14, 2020 01:34
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.

LGTM, non blocking nit/comment


# Global caching for JSON language packs
ALL_LANGUAGE_PACKS: Dict[str, Dict[Any, Any]] = {"en": {}}

DIR = os.path.dirname(os.path.abspath(__file__))


def get_language_pack(locale):
def get_language_pack(locale: str) -> Optional[Dict[Any, Any]]:
Copy link
Member

Choose a reason for hiding this comment

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

I believe we could make the key str to improve differentiation slightly without complicating things too much (strictly speaking it seems the structure is Optional[Dict[str, Union[str, Dict[str, Union[Dict[str, str], List[str]]]]]] which quite frankly looks awful and probably won't help detect any errors)

@john-bodley john-bodley force-pushed the john-bodley--mypy-enforcement-translations branch from 0c8f962 to ad0de37 Compare May 14, 2020 05:09
Copy link
Member

@etr2460 etr2460 left a comment

Choose a reason for hiding this comment

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

one nit, otherwise lgtm

@@ -38,7 +38,7 @@ def get_language_pack(locale):
try:
with open(filename, encoding="utf8") as f:
pack = json.load(f)
ALL_LANGUAGE_PACKS[locale] = pack
ALL_LANGUAGE_PACKS[locale] = pack or {}
except Exception: # pylint: disable=broad-except
# Assuming english, client side falls back on english
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should put:

pack = {}

in this block, and move

ALL_LANGUAGE_PACKS[locale] = pack

outside of the try/except.

Then we could remove the Optional from the get_language_pack return type

Copy link
Member Author

Choose a reason for hiding this comment

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

@etr2460 I thought of that however it would change the return type, i.e., it could no longer be None and per the comment it seems like this could be problematic for the client.

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't an empty dict resolve to false? so if the locale is English then this returns an empty dict (based on the constant at the top of the file)

Copy link
Member

Choose a reason for hiding this comment

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

And this seems to indicate that if this isn't set, we default to an empty object on the frontend anyway: https://github.com/apache-superset/superset-ui/blob/master/packages/superset-ui-translation/src/Translator.ts#L26

Copy link
Member Author

Choose a reason for hiding this comment

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

@etr2460 the idea here was to keep the status quo. pack could be None per line 35 and remain that way if an exception is thrown either when opening the file or parsing the JSON.

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not 100% convinced that it's not worth cleaning up a bit and making this not Optional, but if you don't want to make such a change in the typing PR, that's fine. I can make a follow up PR to update this logic as I think it's cleaner

@john-bodley john-bodley merged commit 53b58ed into apache:master May 17, 2020
@john-bodley john-bodley changed the title style: enforcing mypy typing for translations style(mypy): Enforcing typing for superset.translations module May 29, 2020
auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
Co-authored-by: John Bodley <john.bodley@airbnb.com>
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.37.0 labels Mar 12, 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/S 🚢 0.37.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants