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

Python 3.11 - getdefaultlocale() deprecated #67

Open
vsalvino opened this issue Nov 2, 2022 · 12 comments
Open

Python 3.11 - getdefaultlocale() deprecated #67

vsalvino opened this issue Nov 2, 2022 · 12 comments

Comments

@vsalvino
Copy link

vsalvino commented Nov 2, 2022

Running this package on Python 3.11 yields deprecation warnings:

/opt/hostedtoolcache/Python/3.11.0/x64/lib/python3.11/site-packages/pytest_nunit/nunit.py:119: DeprecationWarning: Use setlocale(), getencoding() and getlocale() instead
    language_code = locale.getdefaultlocale()[0]

See:

(I will try to provide a fix at some point if nobody else takes it up... logging the issue here for memory's sake)

@jamesmyatt
Copy link

I'm not clear about what the fix for this is. Is it just to call locale.getlocale() instead? On my machine, that returns the locale in a different form.

@vsalvino
Copy link
Author

vsalvino commented Jan 9, 2023

After looking into it, I don't actually know what the correct upgrade path is. Admittedly I don't know much about how locales work. Stumbled across this bug which might be worth watching: python/cpython#82986

@mjpieters
Copy link

mjpieters commented Mar 13, 2023

Ideally, you should just use locale.getlocale()[0] instead. Except that that's a hot mess on Windows as vsalvino pointed out.

The other solution is to just.. silence the warning and hope for a better solution before 3.13 comes out:

def _getlocale():
    with warnings.catch_warnings():
        # temporary work-around for https://github.com/python/cpython/issues/82986
        # by continuing to use getdefaultlocale() even though it has been deprecated.
        warnings.simplefilter('ignore', category=DeprecationWarning)
        language_code = locale.getdefaultlocale()[0]
    if language_code:
        return language_code
    return "en-US"

Perhaps inject a warning there if sys.version_info >= (3, 13) is true to make sure this is caught before then?

@MalcolmTyler
Copy link

Changing :
language_code = locale.getdefaultlocale()[0]
to
language_code = locale.getlocale()[0]
worked for me, they both return the same only the first is being deprecated so gives a warning

@mjpieters
Copy link

mjpieters commented May 16, 2023

Changing : language_code = locale.getdefaultlocale()[0] to language_code = locale.getlocale()[0] worked for me, they both return the same only the first is being deprecated so gives a warning

They may give the same for you, but that's not the case for all platforms. See python/cpython#82986 as to why this could be problematic on Windows.

Please read the whole discussion here, as this was already covered.

@MalcolmTyler
Copy link

Changing : language_code = locale.getdefaultlocale()[0] to language_code = locale.getlocale()[0] worked for me, they both return the same only the first is being deprecated so gives a warning

They may give the same for you, but that's not the case for all platforms. See python/cpython#82986 as to why this could be problematic on Windows.

Please read the whole discussion here, as this was already covered.

Fair point, we were seeing the deprecation warning when using pytest and it was only MAC users that were seeing it.

@mathrick
Copy link

locale.setlocale(locale.LC_CTYPE, None) will return a string describing the current locale. Be aware however that it also includes the encoding, ie. en_GB.UTF-8, rather than just en_GB.

@alecglen
Copy link

Does anyone have a temp solution to hide these from the xml output? I'd love to not have 900 lines of this per run in my log.

@mathrick
Copy link

Does anyone have a temp solution to hide these from the xml output? I'd love to not have 900 lines of this per run in my log.

Add the following in your pytest.ini:

filterwarnings =
    ignore:Use setlocale:DeprecationWarning:pytest_nunit

@yuenherny
Copy link

Does anyone have a temp solution to hide these from the xml output? I'd love to not have 900 lines of this per run in my log.

Add the following in your pytest.ini:

filterwarnings =
    ignore:Use setlocale:DeprecationWarning:pytest_nunit

If you are using pyproject.toml, then add this to your pyproject.toml:

[tool.pytest.ini_options]
filterwarnings = [
    "ignore:Use setlocale:DeprecationWarning:pytest_nunit"
]

@tonybaloney
Copy link
Collaborator

Changing : language_code = locale.getdefaultlocale()[0] to language_code = locale.getlocale()[0] worked for me, they both return the same only the first is being deprecated so gives a warning

They may give the same for you, but that's not the case for all platforms. See python/cpython#82986 as to why this could be problematic on Windows.

Please read the whole discussion here, as this was already covered.

I'm going to mute the deprecation warning because getlocale seems fraught with complexity and unresolved issues after reading this and many other related discussions

@tonybaloney
Copy link
Collaborator

tonybaloney commented Feb 13, 2024

Fixed in 1.0.5 by muting deprecation warnings in the existing call. I think the Python API will change before it is fully removed in 3.15

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

No branches or pull requests

8 participants