backports.strenum


Namebackports.strenum JSON
Version 1.3.1 PyPI version JSON
download
home_pagehttps://github.com/clbarnes/backports.strenum
SummaryBase class for creating enumerated constants that are also subclasses of str
upload_time2023-12-09 14:36:40
maintainer
docs_urlNone
authorChris L. Barnes
requires_python>=3.8.6,<3.11
licenseMIT
keywords backports enum strenum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            backports.strenum
=================

A backport of (copy and paste from) python 3.11's ``StrEnum`` class for >=3.8.6:

See the `design discussion <https://discuss.python.org/t/built-in-strenum/4192>`_,
and `Ethan Furman <https://github.com/ethanfurman>`_'s `first <https://github.com/python/cpython/pull/22337>`_ and
`second <https://github.com/python/cpython/pull/22362>`_ PR with this implementation.

A slightly different implementation would likely be compatible with lower python versions;
PRs are welcome if they pass the test suite.
The existing (reference) implementation should still be the one used on supported versions.

Install with ``pip install backports.strenum``, and use with:

.. code-block:: python

    import sys

    if sys.version_info >= (3, 11):
        from enum import StrEnum
    else:
        from backports.strenum import StrEnum

    class MyStrEnum(StrEnum):
        POTATO = "potato"
        ORANGE = "orange"
        SPADE = "spade"

    MyStrEnum.POTATO == "potato"  # True
    MyStrEnum.ORANGE.upper() == "ORANGE"  # True
    str(MyStrEnum.SPADE) == "spade"  # True

From version 1.3.0, this package cannot be installed on python >=3.11.
It shouldn't be used on them anyway.
Make sure that in your package, this is a conditional dependency.

Gotchas
^^^^^^^

A number of behaviours relating to the treatment of enum classes as containers of their members (e.g. iterating and containment checks) will be changing in python 3.12.

This package intends only to allow pre-3.11 users to get 3.11-like behaviour; after that, stick with the standard library.

----

These are the `docs provided with python 3.11 <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_:

``class enum.StrEnum``
^^^^^^^^^^^^^^^^^^^^^^

*StrEnum* is the same as *Enum*, but its members are also strings and can be used in most of the same places that a string can be used.
The result of any string operation performed on or with a *StrEnum* member is not part of the enumeration.

.. Note::
    There are places in the stdlib that check for an exact `str <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ instead of a `str <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ subclass (i.e. ``type(unknown) == str`` instead of ``isinstance(unknown, str)``), and in those locations you will need to use ``str(StrEnum.member)``.

.. Note::
    Using `auto <https://docs.python.org/3.11/library/enum.html#enum.auto>`_ with `StrEnum <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ results in the lower-cased member name as the value.

.. Note::
    `__str__() <https://docs.python.org/3.11/reference/datamodel.html#object.__str__>`_ is str.__str__() to better support the replacement of existing constants use-case. `__format__() <https://docs.python.org/3.11/reference/datamodel.html#object.__format__>`_ is likewise ``str.__format__()`` for that same reason.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/clbarnes/backports.strenum",
    "name": "backports.strenum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.6,<3.11",
    "maintainer_email": "",
    "keywords": "backports,enum,strenum",
    "author": "Chris L. Barnes",
    "author_email": "chrislloydbarnes@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/35/c7/2ed54c32fed313591ffb21edbd48db71e68827d43a61938e5a0bc2b6ec91/backports_strenum-1.3.1.tar.gz",
    "platform": null,
    "description": "backports.strenum\n=================\n\nA backport of (copy and paste from) python 3.11's ``StrEnum`` class for >=3.8.6:\n\nSee the `design discussion <https://discuss.python.org/t/built-in-strenum/4192>`_,\nand `Ethan Furman <https://github.com/ethanfurman>`_'s `first <https://github.com/python/cpython/pull/22337>`_ and\n`second <https://github.com/python/cpython/pull/22362>`_ PR with this implementation.\n\nA slightly different implementation would likely be compatible with lower python versions;\nPRs are welcome if they pass the test suite.\nThe existing (reference) implementation should still be the one used on supported versions.\n\nInstall with ``pip install backports.strenum``, and use with:\n\n.. code-block:: python\n\n    import sys\n\n    if sys.version_info >= (3, 11):\n        from enum import StrEnum\n    else:\n        from backports.strenum import StrEnum\n\n    class MyStrEnum(StrEnum):\n        POTATO = \"potato\"\n        ORANGE = \"orange\"\n        SPADE = \"spade\"\n\n    MyStrEnum.POTATO == \"potato\"  # True\n    MyStrEnum.ORANGE.upper() == \"ORANGE\"  # True\n    str(MyStrEnum.SPADE) == \"spade\"  # True\n\nFrom version 1.3.0, this package cannot be installed on python >=3.11.\nIt shouldn't be used on them anyway.\nMake sure that in your package, this is a conditional dependency.\n\nGotchas\n^^^^^^^\n\nA number of behaviours relating to the treatment of enum classes as containers of their members (e.g. iterating and containment checks) will be changing in python 3.12.\n\nThis package intends only to allow pre-3.11 users to get 3.11-like behaviour; after that, stick with the standard library.\n\n----\n\nThese are the `docs provided with python 3.11 <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_:\n\n``class enum.StrEnum``\n^^^^^^^^^^^^^^^^^^^^^^\n\n*StrEnum* is the same as *Enum*, but its members are also strings and can be used in most of the same places that a string can be used.\nThe result of any string operation performed on or with a *StrEnum* member is not part of the enumeration.\n\n.. Note::\n    There are places in the stdlib that check for an exact `str <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ instead of a `str <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ subclass (i.e. ``type(unknown) == str`` instead of ``isinstance(unknown, str)``), and in those locations you will need to use ``str(StrEnum.member)``.\n\n.. Note::\n    Using `auto <https://docs.python.org/3.11/library/enum.html#enum.auto>`_ with `StrEnum <https://docs.python.org/3.11/library/enum.html#enum.StrEnum>`_ results in the lower-cased member name as the value.\n\n.. Note::\n    `__str__() <https://docs.python.org/3.11/reference/datamodel.html#object.__str__>`_ is str.__str__() to better support the replacement of existing constants use-case. `__format__() <https://docs.python.org/3.11/reference/datamodel.html#object.__format__>`_ is likewise ``str.__format__()`` for that same reason.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Base class for creating enumerated constants that are also subclasses of str",
    "version": "1.3.1",
    "project_urls": {
        "Homepage": "https://github.com/clbarnes/backports.strenum",
        "Repository": "https://github.com/clbarnes/backports.strenum"
    },
    "split_keywords": [
        "backports",
        "enum",
        "strenum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d65056cf20e2ee5127b603b81d5a69580a1a325083e2b921aa8f067da83927c0",
                "md5": "25337266b035a8b43ab84ce9288cec4b",
                "sha256": "cdcfe36dc897e2615dc793b7d3097f54d359918fc448754a517e6f23044ccf83"
            },
            "downloads": -1,
            "filename": "backports_strenum-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "25337266b035a8b43ab84ce9288cec4b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.6,<3.11",
            "size": 8304,
            "upload_time": "2023-12-09T14:36:39",
            "upload_time_iso_8601": "2023-12-09T14:36:39.905844Z",
            "url": "https://files.pythonhosted.org/packages/d6/50/56cf20e2ee5127b603b81d5a69580a1a325083e2b921aa8f067da83927c0/backports_strenum-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "35c72ed54c32fed313591ffb21edbd48db71e68827d43a61938e5a0bc2b6ec91",
                "md5": "32c30ec0cf37836f5482e153a945ba38",
                "sha256": "77c52407342898497714f0596e86188bb7084f89063226f4ba66863482f42414"
            },
            "downloads": -1,
            "filename": "backports_strenum-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "32c30ec0cf37836f5482e153a945ba38",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.6,<3.11",
            "size": 7257,
            "upload_time": "2023-12-09T14:36:40",
            "upload_time_iso_8601": "2023-12-09T14:36:40.937001Z",
            "url": "https://files.pythonhosted.org/packages/35/c7/2ed54c32fed313591ffb21edbd48db71e68827d43a61938e5a0bc2b6ec91/backports_strenum-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-09 14:36:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "clbarnes",
    "github_project": "backports.strenum",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "backports.strenum"
}
        
Elapsed time: 0.47527s