Commit graph

346 commits

Author SHA1 Message Date
Jelle Zijlstra
5707837049
gh-132493: Avoid eager import of annotationlib in typing (again) (#132596)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-04-17 16:03:53 +00:00
Felix Scherz
71af090e24
gh-132493: lazy evaluation of annotations in typing._proto_hook (#132534)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-04-16 08:20:35 -07:00
Jelle Zijlstra
11f6603845
gh-132491: Rename annotationlib.value_to_string to type_repr (#132492) 2025-04-15 20:10:53 +00:00
Jelle Zijlstra
666eeda13d
gh-132493: Support deferred annotations in Protocols (#132494) 2025-04-15 10:14:27 -07:00
Jelle Zijlstra
07b8d3117f
gh-132261: Store annotations at hidden internal keys in the class dict (#132345) 2025-04-10 21:13:26 -07:00
Jelle Zijlstra
305be5fb1a
gh-118761: Lazily import annotationlib in typing (#132060)
annotationlib is used quite a few times in typing.py, but I think the
usages are just rare enough that this makes sense.

The import would get triggered by:
- Using get_type_hints(), evaluate_forward_ref(), and similar introspection
  functions
- Using a string annotation anywhere that goes through _type_convert (e.g.,
  "Final['x']" will trigger an annotationlib import in order to access the
  ForwardRef class).
- Creating a TypedDict or NamedTuple (unless it's empty or PEP 563 is on).

Lots of programs will want to use typing without any of these, so the tradeoff
seems worth it.
2025-04-04 08:57:10 -07:00
Jelle Zijlstra
d1db43c139
gh-105499: Defer "import warnings" in typing (#132061)
A bunch of other warnings in typing.py were already deferred, but
I added a few non-lazy ones.
2025-04-04 06:19:42 -07:00
Jelle Zijlstra
5518c2ae09
gh-128661: Remove DeprecationWarning in evaluate_forward_ref (#128930)
It doesn't make sense to use a deprecation for evaluate_forward_ref,
as it is a new function in Python 3.14 and doesn't have compatibility
guarantees.

I considered making it throw an error if type_params it not passed and
there is no owner. However, I think this is too unfriendly for users. The
case where this param is really needed is fairly esoteric and I don't think
this case is worth the pain of forcing users to write "type_params=()".
2025-04-03 09:52:17 -07:00
Jelle Zijlstra
0f511d8b44
gh-105499: typing: Remove an unused function (#131946)
Leftover from #105511 I believe. GitHub code search found no usages other
than copies of typing.py and lists of stdlib functions.
2025-03-31 10:27:03 -07:00
Victorien
af2afd7c22
gh-119180: Use equality when comparing against annotationlib.Format (#131755) 2025-03-27 21:56:09 -07:00
Sebastian Rittau
c6dd2348ca
gh-127647: Add typing.Reader and Writer protocols (#127648) 2025-03-06 07:36:19 -08:00
Bartosz Sławecki
293fa3433e
gh-85795: Raise a clear error when super() is used in typing.NamedTuple subclasses (#130082) 2025-03-05 20:45:47 -08:00
Jelle Zijlstra
dc6d66f44c
gh-105499: Merge typing.Union and types.UnionType (#105511)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Ken Jin <kenjin@python.org>
Co-authored-by: Carl Meyer <carl@oddbird.net>
2025-03-04 11:44:19 -08:00
sobolevn
b725297cee
gh-128661: Fix typing.evaluate_forward_ref not showing deprecation (#128663)
gh-128661: Fix `typing.evaluate_forward_ref` not showing deprecataion
2025-01-09 18:15:13 +03:00
Serhiy Storchaka
b2ac70a62a
gh-88834: Unify the instance check for typing.Union and types.UnionType (GH-128363)
Union now uses the instance checks against its parameters instead of
the subclass checks.
2024-12-31 10:02:58 +02:00
Jelle Zijlstra
dcf629213b
gh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib (#124415) 2024-11-26 15:40:13 +00:00
Stephen Morton
a83472f49b
gh-126705: Make os.PathLike more like a protocol (#126706)
it can now be used as a base class in other protocols
2024-11-12 17:54:13 +00:00
Stephen Morton
feb3e0b19c
gh-126699: allow AsyncIterator to be used as a base for Protocols (#126702) 2024-11-12 09:17:07 +00:00
Jelle Zijlstra
2c10832887
gh-119180: Rename SOURCE format to STRING (#124620) 2024-09-26 13:49:48 -07:00
Jelle Zijlstra
4e829c0e6f
gh-124412: Add helpers for converting annotations to source format (#124551) 2024-09-26 00:01:09 +00:00
sobolevn
9dacf430c2
Remove unused _allowed_types from typing.py (#124090) 2024-09-14 16:14:45 +03:00
Jelle Zijlstra
016f4b5975
gh-119180: Improvements to ForwardRef.evaluate (#122210)
Noticed some issues while writing documentation for this method.
2024-08-11 23:42:57 +00:00
Xie Yanbo
b6c80e21c7
Fix typos in comments and docstring (#122720)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-08-07 07:39:16 +01:00
Jelle Zijlstra
7b7b90d1ce
gh-119180: Add annotationlib module to support PEP 649 (#119891)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-07-23 21:16:50 +00:00
Alex Waygood
2d3187bf20
gh-114053: Fix another edge case involving get_type_hints, PEP 695 and PEP 563 (#120272) 2024-06-25 16:53:18 +01:00
Jelle Zijlstra
9b8611eeea
gh-119180: PEP 649 compiler changes (#119361) 2024-06-11 13:06:49 +00:00
Jelle Zijlstra
e9875ecb5d
gh-119180: PEP 649: Add __annotate__ attributes (#119209) 2024-05-22 04:38:12 +02:00
Jelle Zijlstra
ec9d12be96
Rename typing._collect_parameters (#118900)
Unfortunately, released versions of typing_extensions
monkeypatch this function without the extra parameter, which makes
it so things break badly if current main is used with typing_extensions.

Fortunately, the monkeypatching is not needed on Python 3.13, because CPython
now implements PEP 696. By renaming the function, we prevent the monkeypatch
from breaking typing.py internals.

We keep the old name (raising a DeprecationWarning) to help other external users who call it.
2024-05-10 16:55:49 +00:00
Nikita Sobolev
2f4db5a04d
gh-118803: Remove ByteString from typing and collections.abc (#118804) 2024-05-09 00:37:55 +01:00
Jelle Zijlstra
aac6b019fe
gh-118772: Allow TypeVars without a default to follow those with a default when constructing aliases (#118774) 2024-05-08 09:54:51 -07:00
Jelle Zijlstra
71080b8a0f
gh-118660: Add second type parameter to (Async)ContextManager (#118681)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-05-07 14:16:05 +00:00
Alex Waygood
0f8a07d158
gh-118418: Deprecate failing to pass a value to the *type_params* parameter of some private typing APIs (#118695) 2024-05-07 11:12:28 +00:00
Jelle Zijlstra
8419f01673
gh-118647: Add defaults to typing.Generator and typing.AsyncGenerator (#118648)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-05-06 22:35:06 +00:00
Serhiy Storchaka
153b3f7530
gh-118465: Add __firstlineno__ attribute to class (GH-118475)
It is set by compiler with the line number of the first line of
the class definition.
2024-05-06 12:02:37 +03:00
Jelle Zijlstra
ca269e58c2
gh-116126: Implement PEP 696 (#116129)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
2024-05-03 06:17:32 -07:00
Nikita Sobolev
4a5ad8469a
gh-118418: Use a default value for type_params in typing._eval_type (#118431) 2024-04-30 15:44:37 +00:00
Jelle Zijlstra
d0b664ee06
gh-118168: Fix Unpack interaction with builtin aliases (#118169)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-04-23 13:40:26 +00:00
Alex Waygood
1e3e7ce11e
gh-114053: Fix bad interaction of PEP-695, PEP-563 and `get_type_hints` (#118009)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2024-04-19 13:03:44 +00:00
Jelle Zijlstra
f2132fcd2a
gh-117516: Implement typing.TypeIs (#117517)
See PEP 742.

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-04-09 10:50:37 +00:00
Jelle Zijlstra
b32789ccb9
gh-117521: Improve typing.TypeGuard docstring (#117522) 2024-04-04 06:39:16 -04:00
傅立业(Chris Fu)
8eec7ed714
gh-117110: Fix subclasses of typing.Any with custom constructors (#117111) 2024-03-29 00:19:20 +00:00
Irit Katriel
79be75735c
gh-115775: Compiler adds __static_attributes__ field to classes (#115913) 2024-03-26 15:18:17 +00:00
Nikita Sobolev
df4784b3b7
gh-116127: PEP-705: Add ReadOnly support for TypedDict (#116350)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2024-03-12 07:49:39 -07:00
Mehdi Drissi
d308d33e09
gh-89547: Support for nesting special forms like Final (#116096) 2024-03-11 23:11:56 -07:00
Nikita Sobolev
a29998a06b
gh-116325: Raise SyntaxError rather than IndexError on ForwardRef with empty string arg (#116341) 2024-03-05 09:14:18 +00:00
Nikita Sobolev
a7549b03ce
gh-112281: Allow Union with unhashable Annotated metadata (#112283)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-03-01 16:19:24 +00:00
dave-shawley
564385612c
gh-115165: Fix typing.Annotated for immutable types (#115213)
The return value from an annotated callable can raise any exception from
__setattr__ for the `__orig_class__` property.
2024-02-09 22:11:37 +00:00
Carl Meyer
a225520af9
gh-112903: Handle non-types in _BaseGenericAlias.__mro_entries__() (#115191)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-02-09 12:19:09 -07:00
Alex Waygood
ed6ea3ea79
gh-113320: Reduce the number of dangerous getattr() calls when constructing protocol classes (#113401)
- Only attempt to figure out whether protocol members are "method members" or not if the class is marked as a runtime protocol. This information is irrelevant for non-runtime protocols; we can safely skip the risky introspection for them.
- Only do the risky getattr() calls in one place (the runtime_checkable class decorator), rather than in three places (_ProtocolMeta.__init__, _ProtocolMeta.__instancecheck__ and _ProtocolMeta.__subclasscheck__). This reduces the number of locations in typing.py where the risky introspection could go wrong.
- For runtime protocols, if determining whether a protocol member is callable or not fails, give a better error message. I think it's reasonable for us to reject runtime protocols that have members which raise strange exceptions when you try to access them. PEP-544 clearly states that all protocol member must be callable for issubclass() calls against the protocol to be valid -- and if a member raises when we try to access it, there's no way for us to figure out whether it's a callable member or not!
2024-01-05 01:01:48 +00:00
Kir
11ee912327
gh-113255: Clarify docs for typing.reveal_type (#113286)
Co-authored-by: AlexWaygood <alex.waygood@gmail.com>
2023-12-20 17:21:23 +00:00