Skip to content

Commit

Permalink
Ignore deprecation warning, with explanation in test file (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacalata authored Jun 12, 2024
1 parent 4c5186d commit 2567208
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions tabcmd/execution/localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ def _load_language(current_locale, domain, logger):


def _get_default_locale():
# c:\dev\tabcmd\tabcmd\execution\localize.py:85: DeprecationWarning 'locale.getdefaultlocale' is deprecated
# see test_localize for details
import logging

logging.captureWarnings(True)
current_locale, encoding = locale.getdefaultlocale()
logging.captureWarnings(False)
current_locale = _validate_lang(current_locale)
return current_locale

Expand Down
20 changes: 19 additions & 1 deletion tests/commands/test_localize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import gettext
import locale
import sys
import unittest
from tabcmd.execution.localize import set_client_locale
from tabcmd.execution.localize import set_client_locale, _get_default_locale


class LocaleTests(unittest.TestCase):
Expand Down Expand Up @@ -44,3 +46,19 @@ def test_en_smoke_line_processed(self):
assert translations is not None

assert translations("importcsvsummary.line.processed") == "Lines processed: {0}"

# https://docs.python.org/3/library/locale.html
# c:\dev\tabcmd\tabcmd\execution\localize.py:85: DeprecationWarning:
# 'locale.getdefaultlocale' is deprecated and slated for removal in Python 3.15. Use setlocale(), getencoding() and getlocale() instead
def test_get_default_locale(self):
# Our method in localize.py that needs to change. An eventual unit test should call this method.
# loc = _get_default_locale() # doesn't return anything: need to mock _validate_lang

# This bug on pytest explains why the proposed replacements aren't directly equivalent.
# Some people online have solved this with manual string mangling. I like the pytest decision
# to wait until we hit 3.15 and hope someone has implemented a better option by then.
# current call that is deprecated -->loc = locale.getdefaultlocale() # returns ('en_US', 'cp1252')
# sys.getdefaultencoding() # returns utf-8
# locale.getlocale() # returns ('English_United States', '1252')
new_locale = locale.setlocale(locale.LC_CTYPE, None) # returns English_United States.125
# assert loc == new_locale

0 comments on commit 2567208

Please sign in to comment.