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.10 deprecation in tests due to distutils #813

Closed
tirkarthi opened this issue May 13, 2021 · 2 comments · Fixed by #830
Closed

Python 3.10 deprecation in tests due to distutils #813

tirkarthi opened this issue May 13, 2021 · 2 comments · Fixed by #830

Comments

@tirkarthi
Copy link
Contributor

The function is simple enough to be vendored for tests.

tests/test_converters.py:7
  /root/checked_repos/attrs/tests/test_converters.py:7: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
    from distutils.util import strtobool

-- Docs: https://docs.pytest.org/en/stable/warnings.html

from distutils.util import strtobool

@sscherfke
Copy link
Contributor

I‘d add this as

def to_bool(val: Any) -> bool:
    """
    Convert "boolean" strings (e.g., from env. vars.) to real booleans.

    Values mapping to :code:`True`:

    - :code:`True`
    - :code:`"true"` / :code:`"t"`
    - :code:`"yes"` / :code:`"y"`
    - :code:`"on"`
    - :code:`"1"`
    - :code:`1`

    Values mapping to :code:`False`:

    - :code:`False`
    - :code:`"false"` / :code:`"f"`
    - :code:`"no"` / :code:`"n"`
    - :code:`"off"`
    - :code:`"0"`
    - :code:`0`

    Raise :exc:`ValueError` for any other value.
    """
    if isinstance(val, str):
        val = val.lower()
    truthy = {True, "true", "t", "yes", "y", "on", "1", 1}
    falsy = {False, "false", "f", "no", "n", "off", "0", 0}
    try:
        if val in truthy:
            return True
        if val in falsy:
            return False
    except TypeError:
        # Raised when "val" is not hashable (e.g., lists)
        pass
    raise ValueError(f"Cannot convert value to bool: {val}")

to converters.py. It could then also be used as field converter.

This function is already part of typed-settings and I intended to eventually move it to attrs.

@hynek, what do you think?

@hynek
Copy link
Member

hynek commented May 13, 2021

SGTM but remember to put the annotations into the stub file. :)

sscherfke added a commit that referenced this issue Jun 5, 2021
sscherfke added a commit that referenced this issue Jun 6, 2021
9999years pushed a commit to 9999years/attrs that referenced this issue Jul 7, 2021
`distutils` is deprecated in Python 3.10 and slated for removal in
Python 3.12. Fortunately, `attrs` only uses `distutils` once and it's
trivial to remove.

As suggested by @sscherfke, add the `to_bool` converter to
`converters.py`.

Closes python-attrs#813

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>
9999years pushed a commit to 9999years/attrs that referenced this issue Jul 7, 2021
`distutils` is deprecated in Python 3.10 and slated for removal in
Python 3.12. Fortunately, `attrs` only uses `distutils` once and it's
trivial to remove.

As suggested by @sscherfke, add the `to_bool` converter to
`converters.py`.

Closes python-attrs#813

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>
hynek added a commit that referenced this issue Aug 10, 2021
* Inline distutils.util.strtobool in tests (#813)

`distutils` is deprecated in Python 3.10 and slated for removal in
Python 3.12. Fortunately, `attrs` only uses `distutils` once and it's
trivial to remove.

As suggested by @sscherfke, add the `to_bool` converter to
`converters.py`.

Closes #813

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>

* Use :raises: directive in docstring

* Remove f-strings for Py2.7 and 3.5 support

* Add to_bool tests

Co-authored-by: Stefan Scherfke <stefan@sofa-rockers.org>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
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 a pull request may close this issue.

3 participants