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

fix: unfreeze pycountry version #22

Merged
merged 9 commits into from
Sep 10, 2018

Conversation

aveuiller
Copy link
Contributor

@aveuiller aveuiller commented Jul 4, 2018

This PR is mostly to fix the compatibility issue with pycountry (country_code being sometimes overridden). But I also tried to refactor the territory management and add a mapping of foreign territories.

I tried to understand and separated as well as possible the concepts you had behind the territories management. It's still far from perfect, but doesn't seem to break the behavior you intended to create. Tell me if it remains understandable on your side.

Please tell me if anything seems off in this PR since it has quite some changes ;)

Closes #16, #11

@remyleone remyleone requested a review from kdeldycke July 4, 2018 10:17
Copy link
Contributor

@kdeldycke kdeldycke left a comment

Choose a reason for hiding this comment

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

I guess you can bump the component to 1.4.0.

'DG': 'IO', # Diego Garcia is part of the British Indian Ocean Territory
'FX': 'FR', # France, Metropolitan
# European Commision country code exceptions.
# Source: http://publications.europa.eu/code/pdf/370000en.htm#pays
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The one you sent here is not working (it is using https and with /en at the beginning). But the one in the code seems to work and has the same content as the web.archive.org link you sent me. Do you want to replace it regardless?

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha! Got it! Just a missing https redirection! No no, keep the original URL! 😄

postal_address/territory.py Show resolved Hide resolved
'UK': 'GB', # United Kingdom
'EL': 'GR', # Greece
'UK': 'GB', # United Kingdom is known as 'GB' in ISO-3166
'EL': 'GR', # 'EL' is the european version of Greece,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you point to https://web.archive.org/web/20171108144027/http://publications.europa.eu/code/pdf/370000en.htm as the reference document that led to that decision?

postal_address/territory.py Show resolved Hide resolved
'FX': 'FR', # France, Metropolitan
# European Commision country code exceptions.
# Source: http://publications.europa.eu/code/pdf/370000en.htm#pays
'EA': 'ES', # 'EA' is the union of Ceuta and Melilla, Spanish territory
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, agree on this one, let's make things simple.

Map some subdivision aliases representing the same territory, but defined
under different countries.

.. data:: FOREIGN_TERRITORIES_ALIAS_TO_COUNTRY
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not rename that kind RESERVED_COUNTRY_CODES? As it it supposed to be an exact copy of special cases exposed at https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Reserved_code_elements right?

@@ -140,7 +146,7 @@ def test_alias_normalization(self):

# Check country alias to a subdivision.
self.assertEquals(
list(territory_parents_codes('TA')),
list(territory_parents_codes('SH-TA')),
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure I really follow you, what's the result of the previous version of the test (a.k.a. territory_parents_codes('TA'))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently it would fail but I can fix it by adding a resolution against COUNTRY_ALIAS_TO_SUBDIVISION in territory_parents. This would send the same result for both queries. Would that be ok ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Great! Sounds fantastic! Let's do that! :)

# Sub-territories will not change by default
self.assertEqual("BQ", normalize_territory_code("BQ"))
self.assertEqual("GP", normalize_territory_code("FR-GP"))
# TODO: Is it normal to not retrieve alpha2 ?
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is allowed as the concept of a "territory" is covering both county and subdivisions. So to me, based on its naming normalize_territory_code() should return the most appropriate territory, be it a country or a subdivision.

self.assertEqual("FR", resolved)

resolved = normalize_territory_code("NL-BQ1",
resole_foreign_territory=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Seeing the usage of that parameter in the wild, I just realized that maybe resole_foreign_territory is badly named and should be called resolve_top_country or something. In which case it will not be allowed to return a subdivision but always a country.

setup.py Outdated
@@ -31,7 +31,7 @@
# TODO: subdivision definitions are broken for Czech Republic starting with
# PyCountry 16.11.27. See:
# https://bitbucket.org/flyingcircus/pycountry/issues/13389
'pycountry >= 16.11.08, < 16.11.27',
'pycountry >= 18.5.26',
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you can remove the TODO statement above then.

@codecov-io
Copy link

codecov-io commented Jul 4, 2018

Codecov Report

Merging #22 into develop will increase coverage by 0.39%.
The diff coverage is 98.36%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop      #22      +/-   ##
===========================================
+ Coverage    95.61%   96.01%   +0.39%     
===========================================
  Files            3        2       -1     
  Lines          342      376      +34     
  Branches        91       95       +4     
===========================================
+ Hits           327      361      +34     
  Misses           5        5              
  Partials        10       10
Impacted Files Coverage Δ
postal_address/address.py 96.51% <100%> (+0.29%) ⬆️
postal_address/territory.py 94.91% <96.87%> (+0.97%) ⬆️
postal_address/tests/__init__.py

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 7b48a43...5999562. Read the comment docs.

@@ -140,7 +146,7 @@ def test_alias_normalization(self):

# Check country alias to a subdivision.
self.assertEquals(
list(territory_parents_codes('TA')),
list(territory_parents_codes('SH-TA')),
Copy link
Contributor

Choose a reason for hiding this comment

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

Great! Sounds fantastic! Let's do that! :)

'{}_code'.format(subdiv_type_id): subdivision.code,
# Rename code to slug to avoid overriding 'country_code' in some cases
# See https://github.com/scaleway/postal-address/issues/16
'{}_slug'.format(subdiv_type_id): subdivision.code,
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that I saw the use of that metadata in the wild, _slug might not be the right word, as it still holds a code. Don't you think _key, _area_code will be more appropriate here?

@kdeldycke
Copy link
Contributor

Hey @aveuiller, what's the status of that PR? Do you want me to merge it?

@aveuiller
Copy link
Contributor Author

Hello, yes this pull request is finished on my side. :)

@kdeldycke
Copy link
Contributor

Let's merge then! Thanks @aveuiller for your patience and huge effort! 👍

@kdeldycke kdeldycke merged commit c309cf3 into scaleway:develop Sep 10, 2018
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.

Edge cases in country/state normalization
3 participants