Skip to content

make ParamSpecFlavor an IntEnum and ParamSpecType generic in that type. #19670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
import sys
from abc import abstractmethod
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Any, ClassVar, Final, NewType, TypeVar, Union, cast, overload
from typing_extensions import Self, TypeAlias as _TypeAlias, TypeGuard
from enum import IntEnum
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Final,
Generic,
Literal,
NewType,
Union,
cast,
overload,
)
from typing_extensions import Self, TypeAlias as _TypeAlias, TypeGuard, TypeVar

import mypy.nodes
from mypy.bogus_type import Bogus
Expand Down Expand Up @@ -694,16 +706,19 @@ def deserialize(cls, data: JsonDict) -> TypeVarType:
)


class ParamSpecFlavor:
class ParamSpecFlavor(IntEnum):
# Simple ParamSpec reference such as "P"
BARE: Final = 0
BARE = 0
# P.args
ARGS: Final = 1
ARGS = 1
# P.kwargs
KWARGS: Final = 2
KWARGS = 2


Flavor = TypeVar("Flavor", bound=ParamSpecFlavor, default=Any)


class ParamSpecType(TypeVarLikeType):
class ParamSpecType(TypeVarLikeType, Generic[Flavor]):
"""Type that refers to a ParamSpec.

A ParamSpec is a type variable that represents the parameter
Expand All @@ -721,17 +736,22 @@ class ParamSpecType(TypeVarLikeType):
always just 'object').
"""

# convenience type aliases
BARE: _TypeAlias = "ParamSpecType[Literal[ParamSpecFlavor.BARE]]"
ARGS: _TypeAlias = "ParamSpecType[Literal[ParamSpecFlavor.ARGS]]"
KWARGS: _TypeAlias = "ParamSpecType[Literal[ParamSpecFlavor.KWARGS]]"

__slots__ = ("flavor", "prefix")

flavor: int
flavor: Flavor
prefix: Parameters

def __init__(
self,
name: str,
fullname: str,
id: TypeVarId,
flavor: int,
flavor: Flavor,
upper_bound: Type,
default: Type,
*,
Expand All @@ -740,10 +760,12 @@ def __init__(
prefix: Parameters | None = None,
) -> None:
super().__init__(name, fullname, id, upper_bound, default, line=line, column=column)
self.flavor = flavor
self.flavor = ParamSpecFlavor(flavor) # type: ignore[assignment]
self.prefix = prefix or Parameters([], [], [])

def with_flavor(self, flavor: int) -> ParamSpecType:
_F = TypeVar("_F", bound=ParamSpecFlavor, default=Flavor)

def with_flavor(self, flavor: _F) -> ParamSpecType[_F]:
return ParamSpecType(
self.name,
self.fullname,
Expand All @@ -758,16 +780,16 @@ def copy_modified(
self,
*,
id: Bogus[TypeVarId] = _dummy,
flavor: int = _dummy_int,
flavor: _F = _dummy_int, # type: ignore[assignment]
prefix: Bogus[Parameters] = _dummy,
default: Bogus[Type] = _dummy,
**kwargs: Any,
) -> ParamSpecType:
) -> ParamSpecType[_F]:
return ParamSpecType(
self.name,
self.fullname,
id if id is not _dummy else self.id,
flavor if flavor != _dummy_int else self.flavor,
flavor if flavor != _dummy_int else self.flavor, # type: ignore[arg-type]
self.upper_bound,
default=default if default is not _dummy else self.default,
line=self.line,
Expand Down
Loading