Skip to content

Remove deprecated sre_* modules #105456

Description

@sobolevn

Feature or enhancement

sre_* modules like sre_constants, sre_compile, and sre_parse were deprecated in 3.11 in 1be3260

Our regular deprecation policy is that the deprecated things can be removed in N + 2 release, which is 3.13.

Pitch

Let's remove them if there are no objections.

I guess it is safe to remove them for several reasons:

  1. https://bugs.python.org/issue47152 clearly states that they were undocumented
  2. There are now re._parser, re._constants, and re._compiler modules that are used instead
  3. They were listed as deprecated in the "what's new" and the warning was pretty clear

The only argument agaist removing them:

  1. The deprecation warning never says when they will be removed:
>>> import sre_compile
<stdin>:1: DeprecationWarning: module 'sre_compile' is deprecated

I will send a PR once we settle it:

  • Either with a better deprecation message
  • Or with these modules removed

@vstinner @serhiy-storchaka what's your opinion?

Linked PRs

Activity

  1. self-assigned this
    on Jun 7, 2023
  2. hugovk commented on Jun 7, 2023

    @hugovk
    Member

    Do they have much use in the top 5k PyPI packages?

    For example: https://dev.to/hugovk/how-to-search-5000-python-projects-31gk

  3. sobolevn commented on Jun 7, 2023

    @sobolevn
    MemberAuthor

    It is still used, yes.
    But, many results here are from mypy / typeshed / jedi usages, which are safe.
    There are some string usages, like in isort, pipreqs, ruff, and in one pytest plugin, which are also safe: for example, pytest does not recognise sre_* modules as stdlib ones.

    Notice, that some results are from vendored dependencies that would be quite hard to update.

    Full results of cpython/search_pypi_top.py -q . "sre_(compile|constants|parse)" > results.txt
    results.txt

  4. vstinner commented on Jun 7, 2023

    @vstinner
    Member

    It's interesting that exrex already uses re._parser. This project is: "Irregular methods for regular expressions.".

    ./exrex-0.11.0.tar.gz: exrex-0.11.0/exrex.py: import re._parser as sre_parse
    ./exrex-0.11.0.tar.gz: exrex-0.11.0/exrex.py: from re import sre_parse
    

    Another project is using the private re._constants sub-module (people love to abuse the private API instead of asking to make what they need public):

    ./rstr-3.2.1.tar.gz: rstr-3.2.1/rstr/xeger.py: import re._parser as sre_parse  # type: ignore[import]
    ./rstr-3.2.1.tar.gz: rstr-3.2.1/rstr/xeger.py: import sre_parse  # type: ignore[no-redef]
    ./rstr-3.2.1.tar.gz: rstr-3.2.1/rstr/xeger.py: parsed = sre_parse.parse(pattern)
    

    Another example:

    ./catboost-1.2.tar.gz: catboost-1.2/catboost_all_src/contrib/python/hypothesis/py3/hypothesis/strategies/_internal/regex.py: import re._parser as sre_parse
    

    pyparsing was using sre_constants, it's no longer the case:

    Version 3.0.8 - April, 2022
    ---------------------------
    (...)
    - Removed imports of deprecated `sre_constants` module for catching
      exceptions when compiling regular expressions. PR submitted by
      Serhiy Storchaka, thank you.
    

    There are vendored copies of pyparsing which still use sre_constants.

    coverage test suite explicitly ignores the deprecation, tests/conftest.py:

        warnings.filterwarnings(
            "ignore",
            category=DeprecationWarning,
            message=r"module 'sre_constants' is deprecated",
        )

    Similar example:

    ./pydantic_factories-1.17.3.tar.gz: pydantic_factories-1.17.3/CHANGELOG.md: - fix deprecation warning for `sre_parse` on Python 3.11.
    ./pydantic_factories-1.17.3.tar.gz: pydantic_factories-1.17.3/pydantic_factories/value_generators/regex.py: from sre_parse import SubPattern, parse  # pylint: disable=deprecated-module
    

    or:

    ./trio-0.22.0.tar.gz: trio-0.22.0/trio/tests/test_exports.py: "ignore:module 'sre_constants' is deprecated:DeprecationWarning",
    
  5. vstinner commented on Jun 8, 2023

    @vstinner
    Member

    I would prefer that people don't use private APIs. But well, as soon as it's technically possible, people just continue to do that.

    Do they have much use in the top 5k PyPI packages?

    At the least, it's unclear to me if it's ok or not to remove these modules right now. I would say that it's ok since we respected PEP 387 deprecation period. But @serhiy-storchaka may have a different opinion.

  6. serhiy-storchaka commented on Jun 8, 2023

    @serhiy-storchaka
    Member

    Initially I was going to not keep old modules. But it was shown that they are used in some third-party projects, so it would be nicer from us to keep them for a time. Now we can remove them at any time. But it would be even more nicer if we first add public API for things used in third-party code. It is not always feasible, for example re._constants is inherently implementation detail. But I was going to add public API for compiling replacement strings.

  7. StanFromIreland commented on Jun 26, 2025

    @StanFromIreland
    Member

    Searching the top (500) pypi projects I find that the only concerning use is from isort. In general it seems most projects use the re modules.

    How about we hard deprecate for 3.16?

  8. vstinner commented on Jun 26, 2025

    @vstinner
    Member

    How about we hard deprecate for 3.16?

    What do you mean by hard deprecation? For example, sre_constants already emits a DeprecationWarning:

    $ python3.13 -Wd -c 'import sre_constants'
    <string>:1: DeprecationWarning: module 'sre_constants' is deprecated
    
  9. StanFromIreland commented on Jun 26, 2025

    @StanFromIreland
    Member

    What do you mean by hard deprecation?

    From my understanding it is moving it from the deprecated stage to the pending removal in a defined version stage, i.e.

    $ python3.15 -Wd -c 'import sre_constants'
    <string>:1: DeprecationWarning: module 'sre_constants' is deprecated and will be removed in 3.16
    
  10. vstinner commented on Jun 26, 2025

    @vstinner
    Member

    Alright, I see that the 3 sre modules are listed in "Pending removal in future versions", so no scheduled version: https://docs.python.org/dev/whatsnew/3.14.html#pending-removal-in-future-versions

    Scheduling the removal in a specific version should help users to decide when to handle these deprecations.

  11. StanFromIreland commented on Jun 26, 2025

    @StanFromIreland
    Member

    I will write a PR setting it to 3.16 unless you prefer a later version, it has been long enough IMO?

  12. hugovk commented on Jun 26, 2025

    @hugovk
    Member

    Searching the top (500) pypi projects I find that the only concerning use is from isort.

    This use is fine, isort has lists of known modules in each Python version.

    Scheduling the removal in a specific version should help users to decide when to handle these deprecations.

    Yep, we could even remove them now, but @serhiy-storchaka was planning to add some public API: #105456 (comment). Any news?

  13. serhiy-storchaka commented on Jun 26, 2025

    @serhiy-storchaka
    Member

    See #135992.

    It is good tone to only break the old way if the new way exists in few versions. But since sre_* modules were undocumented and they have been deprecated for several versions, we can remove them now.

  14. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 27, 2025
  15. added a commit that references this issue on Jul 1, 2025
  16. vstinner commented on Jul 1, 2025

    @vstinner
    Member

    The 3 modules have been removed by the change 93809a9.

  17. added a commit that references this issue on Jul 11, 2025
  18. added a commit that references this issue on Jul 12, 2025
  19. added a commit that references this issue on Jul 13, 2025
  20. added a commit that references this issue on Aug 4, 2025
  21. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-regextype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions