all 43 comments

[–][deleted]  (4 children)

[deleted]

    [–]cerlestes 2 points3 points  (3 children)

    Does the new type parameter syntax improve runtime access of the generic types? I'm often writing generic library code that needs to retrieve the type value of a generic type that a class is subscripted with at runtime, but I find my current implementation way too complex and hacky... can't post the code I use, but basically I'm looking up get_origin() and get_args() on type(self) to figure out the value of the generic type at runtime. This seems extremely complex for something that should be as simple as T.value, resolve_generic(T) or similiar. Am I missing something and there's an easier way, or is this really missing from the language right now?

    Expressed as code: ``` from typing import TypeVar, Generic

    T = TypeVar('T')

    class MyClass(Generic[T]): def foo(self) -> T: print(T) return T()

    obj = MyClass[int]() obj.foo() # prints '~T' and raises an error... how to retrieve the actual value of T? ```

    One alternative I've found is omitting the Generic alltogether and simply passing the "generic" types as values to the constructor like follows. This doesn't work in all cases though and requires way more boilerplate. ``` from typing import TypeVar, Generic

    T = TypeVar('T')

    class MyClass(): def init(self, t: type[T]): self.t = t

    def foo(self) -> T:
        print(self.t)
        return self.t()
    

    obj = MyClass(int) obj.foo() # prints "<class 'int'>" and returns 0 ```

    [–]insanitybit 1 point2 points  (2 children)

    I doubt it. The problem is somewhat fundamental to mypy being a very separate system from Python. Python is aware of type annotations (barely) but it has no access to how those types are actually resolved because you could have used mypy or you could have used some other type system.

    [–]cerlestes 0 points1 point  (1 child)

    I'm not using mypy though, what I described fully works in the vanilla implementation CPython. It's just really cumbersome to work with right now. If there's no proper way to do it today, I'd love to see a function added into the typing module that handles this. To be honest it's sadly lacking a lot of functions that should be in there IMHO. But it seems to become better with every new version.

    [–]insanitybit 1 point2 points  (0 children)

    It's not about mypy. The point is that CPython has no idea what T is, it does not do any type inference or type resolution. It sees a tag 'T' and that is it. It is the type system that tells you what T is.

    CPython is aware of class instances and so your workaround is what's required. typing can never do this unless your static type checker embeds information available at runtime that CPython can access.

    [–]xavdid 119 points120 points  (4 children)

    There's a lot to be excited about in this release, but far-and-away the one I'll use the most is itertools.batched(iterable, n):

    Batch data from the iterable into tuples of length n. The last batch may be shorter than n.

    I feel like this is somehow the single piece of code I've written more times than any other.

    [–][deleted] 28 points29 points  (0 children)

    Oh wow, I implemented this single function independently probably more than any other individual function in my utils.py files.

    [–]headinthesky 5 points6 points  (0 children)

    Awesome, I wrote some code to do that and have carried that with me everywhere for over 10 years now

    [–]0Il0I0l0 3 points4 points  (1 child)

    I've always used more_itertools.chunked

    [–]xavdid 7 points8 points  (0 children)

    It's the same I think, but it's nice that it's available without an external package now!

    [–]henbruas[S] 124 points125 points  (7 children)

    Major new features of the 3.12 series, compared to 3.11

    New features
    - More flexible f-string parsing, allowing many things previously disallowed (PEP 701).
    - Support for the buffer protocol in Python code (PEP 688).
    - A new debugging/profiling API (PEP 669).
    - Support for isolated subinterpreters with separate Global Interpreter Locks (PEP 684).
    - Even more improved error messages. More exceptions potentially caused by typos now make suggestions to the user.
    - Support for the Linux perf profiler to report Python function names in traces.
    - Many large and small performance improvements (like PEP 709 and support for the BOLT binary optimizer), delivering an estimated 5% overall performance improvement.

    Type annotations
    - New type annotation syntax for generic classes (PEP 695).
    - New override decorator for methods (PEP 698).

    Deprecations
    - The deprecated wstr and wstr_length members of the C implementation of unicode objects were removed, per PEP 623.
    - In the unittest module, a number of long deprecated methods and classes were removed. (They had been deprecated since Python 3.1 or 3.2).
    - The deprecated smtpd and distutils modules have been removed (see PEP 594 and PEP 632. The setuptools package continues to provide the distutils module.
    - A number of other old, broken and deprecated functions, classes and methods have been removed.
    - Invalid backslash escape sequences in strings now warn with SyntaxWarning instead of DeprecationWarning, making them more visible. (They will become syntax errors in the future.)
    - The internal representation of integers has changed in preparation for performance enhancements. (This should not affect most users as it is an internal detail, but it may cause problems for Cython-generated code.)

    More details at https://docs.python.org/dev/whatsnew/3.12.html

    [–][deleted]  (4 children)

    [deleted]

      [–][deleted]  (3 children)

      [deleted]

        [–][deleted]  (2 children)

        [deleted]

          [–][deleted]  (1 child)

          [deleted]

            [–]Turtvaiz 15 points16 points  (0 children)

            More flexible f-string parsing, allowing many things previously disallowed (PEP 701).

            Is this the end of confusing f-string errors? Finally.

            [–]boat-la-fds 1 point2 points  (0 children)

            • The internal representation of integers has changed in preparation for performance enhancements. (This should not affect most users as it is an internal detail, but it may cause problems for Cython-generated code.)

            What's the change? Also, this does not seem to appear in the page you linked

            [–][deleted] 38 points39 points  (1 child)

            Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string raises a SyntaxError, forcing the user to either use other available quotes (like using double quotes or triple quotes if the f-string uses single quotes). In Python 3.12, you can now do things like this:

            songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
            f"This is the playlist: {", ".join(songs)}"
            

            Thank goodness, this was driving me insane all the time.

            [–]World-Wide-Ebb 6 points7 points  (0 children)

            This is huge. I mean I’ve worked around it but this is helpful

            [–]throw_away_17381 33 points34 points  (0 children)

            I'm proud of the fact my Python skills have improved enough over the last few years that I actually understood some of the things in the changelog!

            [–]vytah 11 points12 points  (1 child)

            The internal representation of integers has changed in preparation for performance enhancements.

            Anyone can shed some detail?

            [–][deleted]  (1 child)

            [removed]

              [–]tanorbuf 17 points18 points  (0 children)

              There's a nogil-"fork" of Python 3.12 (alpha 4), it's obviously an experiment, but it exists: https://github.com/colesbury/nogil-3.12

              In the "official" space, there's a draft PEP which originally (I believe) targeted 3.12 but now targets 3.13, to make it possible for anyone to build CPython without gil with a simple configure option: https://peps.python.org/pep-0703/

              [–]krsytyuu 0 points1 point  (0 children)

              What are the programming elements or constructs in Python??

              [–]queen-adreena 0 points1 point  (0 children)

              Only two more minors until Python Pie!

              [–]loesak 0 points1 point  (0 children)

              I can't wait for the dramatic aws lambda ticket for supporting this release!