python / python/mypy

Enum member aliases does not work correctly with Literal

Đang mở
#8,657 6 bình luận 7 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic-enum
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

I'm not entirely sure whether this is a bug or missing feature related to the literals and enums but I think it is worth noticing.

Let's say that we have an enum and we define aliases for enum member outside of the enum's scope:

import enum

from typing import Literal

class Color(enum.Enum):

    BLACK = enum.auto()

BLACK = Color.BLACK
BLACK_ALIAS: Literal[Color.BLACK] = Color.BLACK

reveal_type(Color.BLACK)
reveal_type(BLACK)
reveal_type(BLACK_ALIAS)

Running mypy on above code results in following output:

example.py:12: note: Revealed type is 'Literal[Color.BLACK]?'
example.py:13: note: Revealed type is 'Color'
example.py:14: note: Revealed type is 'Literal[Color.BLACK]'

Now it's not idea that second reveal does not reveal Color.BLACK but it's also not that much of a problem since we can add Literal[Color.BLACK typehint and the see the expected result in third reveal. The bigger problem comes, when we want to use Literal on such aliases:

import enum

from typing import Literal, Optional

class Color(enum.Enum):

    BLACK = enum.auto()

BLACK = Color.BLACK
BLACK_ALIAS: Literal[Color.BLACK] = Color.BLACK

x: Optional[Literal[Color.BLACK]] = None
y: Optional[Literal[BLACK]] = None
z: Optional[Literal[BLACK_ALIAS]] = None

reveal_type(x)
reveal_type(y)
reveal_type(z)

Running mypy on above code results in following output:

example.py:16: note: Revealed type is 'Union[Literal[Color.BLACK], None]'
example.py:17: note: Revealed type is 'Union[Any, None]'
example.py:18: note: Revealed type is 'Union[Any, None]'

It's clear that mypy does not interfere those aliases correctly even those the BLACK_ALIAS seemed to be correctly revealed as Literal[Color.BLACK] previously. It would be really nice to have mypy reveal those types correctly.

Now, you might ask, why would you want to define such aliases in the first place? Well, the most probable scenario would be the idea of having a enum class that basically implements a null object pattern. In such cases, you probably don't want to expose the enum class itself to the user.

The most basic example would be this:

import enum

from typing import Literal

class _MissingType(enum.Enum):

    MISSING = enum.auto()

    def __repr__(self) -> str:
        return self.name

MISSING: Literal[_MissingType.MISSING] = _MissingType.MISSING

Now with such construct I don't want to expose _MissingType class but because MISSING alias is not always interfered correctly I cannot do that if I want to have correct typing information everywhere.

I came up with following "reasonable" workaround for the time being:

MissingLiteral = Literal[_MissingType.MISSING]

Which allows me to only expose MissingLiteral and use it in following way:

x: Optional[MissingLiteral] = None

The revealed type is of course Union[Literal[Color.BLACK], None]

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện hai ví dụ trong example.py và so sánh các kiểu được mypy tiết lộ cho Color.BLACK, BLACK, BLACK_ALIAS và các chú thích Optional[Literal[...]]. Theo dõi các đường dẫn suy luận của enum và Literal, sau đó thêm kiểm thử hồi quy cho thấy các bí danh giữ nguyên kiểu literal của enum; công việc được hoàn thành khi các kiểu được tiết lộ dựa trên bí danh khớp với kết quả trực tiếp của Color.BLACK.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
developer-experience, tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.