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

Ignore deprecation warning, with explanation in test file #291

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading