GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Deprecate creating immutable types with mutable bases · Issue #95388 · python/cpython

Deprecate creating immutable types with mutable bases #95388

New issue
Copy link
New issue
Copy link
Closed
Closed
Deprecate creating immutable types with mutable bases#95388
Copy link
Assignees
encukou
Labels
topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@encukou
encukou
opened on Jul 28, 2022
Issue body actions

The C API currently allows creating immutable types from mutable bases. I can't find a case where this breaks things, but it's very hard to think about (and easy to skip thinking about) how this edge case affects things like cache invalidation.

I also don't immediately see a use case for this, so I think it's best to deprecate it, wait for 2 releases to see if anyone needs it, and remove it.

Reacted by Donghee Na and Erlend E. Aasland

Activity

  1. encukou
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-C-API
    on Jul 28, 2022
  2. encukou
    self-assigned this
    on Jul 28, 2022
  3. rhettinger commented on Jul 29, 2022

    @rhettinger
    rhettinger
    on Jul 29, 2022
    Contributor
    More actions

    How are you detecting mutability? My understanding is that mutability arises solely through the addition of mutating methods and that in the general case this isn't easily detected.

    Also, isn't it a valid pattern to subclass a mutable class like dict and then override the mutating methods to disable them and create an immutable class? IIRC, there are frozendict implementations that do exactly this.

    Reacted by Maarten Derickx
  4. encukou commented on Jul 30, 2022

    @encukou
    encukou
    on Jul 30, 2022
    MemberAuthor
    More actions

    This is about type objects themselves, not instances – e.g. the str type is immutable so you can't set str.foo.
    Immutable types have Py_TPFLAGS_IMMUTABLETYPE set.

    Reacted by Erlend E. Aasland
  5. vstinner commented on Aug 1, 2022

    @vstinner
    vstinner
    on Aug 1, 2022
    Member
    More actions

    The C API currently allows creating immutable types from mutable bases

    Ah? Would you mind to elaborate? Do you have an example?

    Are you talking about defining a heap type which inherits from a heap type or a static type?


    type_ready_mro() implements the following check. Does it prevent the issue for static types at least?

        /* All bases of statically allocated type should be statically allocated */
        if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
            PyObject *mro = type->tp_mro;
            Py_ssize_t n = PyTuple_GET_SIZE(mro);
            for (Py_ssize_t i = 0; i < n; i++) {
                PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(mro, i));
                if (base->tp_flags & Py_TPFLAGS_HEAPTYPE) {
                    PyErr_Format(PyExc_TypeError,
                                 "type '%.100s' is not dynamically allocated but "
                                 "its base type '%.100s' is dynamically allocated",
                                 type->tp_name, base->tp_name);
                    return -1;
                }
            }
        }
  6. encukou
    added a commit that references this issue on Aug 1, 2022

    pythongh-95388: Deprecate creating immutable types with mutable bases

    c21b016
  7. encukou commented on Aug 1, 2022

    @encukou
    encukou
    on Aug 1, 2022
    MemberAuthor
    More actions

    @vstinner, this test that can serve as an example: https://github.com/python/cpython/pull/95533/files#diff-a537c794ffb51531649e2b81f9ae6978c25077de6aa10c9134576ac628fcc345R629-R654

    Yes, static types aren't affected. Only heap types can be mutable.

  8. encukou
    added a commit that references this issue on Aug 4, 2022

    gh-95388: Deprecate creating immutable types with mutable bases (GH-9…

    a613fed
  9. encukou
    closed this as completedon Aug 4, 2022
  10. serhiy-storchaka commented on Aug 4, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 4, 2022
    Member
    More actions

    Py_TPFLAGS_IMMUTABLETYPE was added to simplify the conversion of static types to heap types. Inheriting static types from heap types was forbidden (I do not remember exact reason but it is likely that it was the same reason as of this issue). Perhaps allowing inheriting type with a heap type was Py_TPFLAGS_IMMUTABLETYPE was a mistake. And since it is a new feature, should not we just disallow it instead of emitting a warning?

  11. encukou commented on Aug 4, 2022

    @encukou
    encukou
    on Aug 4, 2022
    MemberAuthor
    More actions

    It's not a new feature any more. I'd rather go through the deprecation cycle than try to prove no one is using this.

  12. erlend-aasland
    added a commit that references this issue on Aug 5, 2022

    pythongh-95388: Suppress deprecation warning in test_immutable_type_w…

    e2df21d
  13. encukou
    added a commit that references this issue on Aug 8, 2022

    gh-95388: Suppress deprecation warning in test_immutable_type_with_mu…

    78a85a3
  14. iritkatriel
    added 2 commits that reference this issue on Aug 11, 2022

    pythongh-95388: Deprecate creating immutable types with mutable bases (…

    9a9d043

    pythongh-95388: Suppress deprecation warning in test_immutable_type_w…

    027ad26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @encukou
    encukou

Labels

topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.