Skip to content

Decide the fate of type_params in _eval_type and _evaluate #136021

Open
@sobolevn

Description

@sobolevn

Feature or enhancement

Currently there are two places in our typing-related code that raise a DeprecationWarning when type_params parameter is not provided:

cpython/Lib/typing.py

Lines 432 to 442 in 07183eb

def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=frozenset(),
format=None, owner=None):
"""Evaluate all forward references in the given type t.
For use of globalns and localns see the docstring for get_type_hints().
recursive_guard is used to prevent infinite recursion with a recursive
ForwardRef.
"""
if type_params is _sentinel:
_deprecation_warning_for_no_type_params_passed("typing._eval_type")
type_params = ()

def _evaluate(self, globalns, localns, type_params=_sentinel, *, recursive_guard):
import typing
import warnings
if type_params is _sentinel:
typing._deprecation_warning_for_no_type_params_passed(
"typing.ForwardRef._evaluate"
)
type_params = ()

The error message of this function says that the behavior will change in 3.15 to disallow this:

cpython/Lib/typing.py

Lines 410 to 420 in 07183eb

def _deprecation_warning_for_no_type_params_passed(funcname: str) -> None:
import warnings
depr_message = (
f"Failing to pass a value to the 'type_params' parameter "
f"of {funcname!r} is deprecated, as it leads to incorrect behaviour "
f"when calling {funcname} on a stringified annotation "
f"that references a PEP 695 type parameter. "
f"It will be disallowed in Python 3.15."
)
warnings.warn(depr_message, category=DeprecationWarning, stacklevel=3)

So, we have several options:

  1. Adding TypeError('type_params parameter is not provided') when type_params is _sentinel
  2. Changing type_params to not contain a default value
  3. Do nothing?

CC @JelleZijlstra @AlexWaygood

I would like to work on this after we decide the fate of these functions :)

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtopic-typingtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions