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

Utilize __init_subclass__ instead of inspect on Locale mapping to improve type checking #920

Merged
merged 3 commits into from
Apr 8, 2021
Merged

Utilize __init_subclass__ instead of inspect on Locale mapping to improve type checking #920

merged 3 commits into from
Apr 8, 2021

Conversation

isac322
Copy link
Contributor

@isac322 isac322 commented Feb 18, 2021

Pull Request Checklist

  • 🧪 Added tests for changed code.
  • 🛠️ All tests pass when run locally (run tox or make test to find out!).
  • 🧹 All linting checks pass when run locally (run tox -e lint or make lint to find out!).
  • 📚 Updated documentation for changed code.
  • ⏩ Code is up-to-date with the master branch.

Description of Changes

Python 3.6 introduced __init_subclass__ that invoked when any subclass is declared. It can replace behavior of arrow.locales._map_locales().

Additional feature

__init_subclass__ invoked on every subclassing -> Subclass of Locale outside of arrow package also registered on arrow.locales._locale_map.

Example

>>> from arrow.locales import Locale
>>> class Locale1(Locale):
...   names = ['asdf']
... 
>>> from arrow import locales
>>> locales.get_locale('asdf')
<__main__.Locale1 object at 0x100f19b50>

@codecov
Copy link

codecov bot commented Feb 18, 2021

Codecov Report

Merging #920 (44cd011) into master (485d40b) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #920      +/-   ##
==========================================
- Coverage   99.74%   99.74%   -0.01%     
==========================================
  Files          10       10              
  Lines        1937     1934       -3     
  Branches      313      312       -1     
==========================================
- Hits         1932     1929       -3     
  Misses          4        4              
  Partials        1        1              
Impacted Files Coverage Δ
arrow/locales.py 100.00% <100.00%> (ø)

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 485d40b...44cd011. Read the comment docs.

Copy link
Member

@krisfremen krisfremen left a comment

Choose a reason for hiding this comment

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

One comment from me, otherwise, I don't see anything wrong from a first glance.

Will take a better look later today.

arrow/locales.py Show resolved Hide resolved
@systemcatch systemcatch changed the title [Refactor locales] Utilize __init_subclass__ instead of inspect on Locale mapping Utilize __init_subclass__ instead of inspect on Locale mapping to improve type checking Feb 28, 2021
Copy link
Member

@jadchaar jadchaar left a comment

Choose a reason for hiding this comment

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

LGTM but had a small comment

@@ -121,6 +123,13 @@ class Locale:

_month_name_to_ordinal: Optional[Dict[str, int]]

def __init_subclass__(cls, **kwargs: Any) -> None:
for locale_name in cls.names:
if locale_name in _locale_map:
Copy link
Member

Choose a reason for hiding this comment

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

Should we maintain the locale_name.lower() like in the old code?

@jadchaar jadchaar merged commit 6941e32 into arrow-py:master Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants