Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Jan 27, 2026

This PR should not change semantics

We refactor to avoid the separate loops for type_targets and value_targets, which allows the logic to be more consolidated, helps with future changes and is probably faster

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@hauntsaninja
Copy link
Collaborator Author

Merging, but comments are very welcome!

@hauntsaninja hauntsaninja merged commit 60d136e into python:master Jan 28, 2026
23 checks passed
@hauntsaninja hauntsaninja deleted the narrow31 branch January 28, 2026 00:04
@cdce8p
Copy link
Collaborator

cdce8p commented Jan 28, 2026

This change seems to have introduced a small regression.

from enum import StrEnum

class Capability(StrEnum):
    COLOR_CONTROL = "colorControl"
    COLOR_TEMPERATURE = "colorTemperature"

def func(capability: Capability | str):
    if capability in (Capability.COLOR_CONTROL, Capability.COLOR_TEMPERATURE):
        reveal_type(capability)
# before
Capability

# now
Capability | str

It reverses part of the improvement from #20602.

@hauntsaninja
Copy link
Collaborator Author

Thanks for the post, but this is actually an improvement!

from enum import StrEnum

class Capability(StrEnum):
    COLOR_CONTROL = "colorControl"
    COLOR_TEMPERATURE = "colorTemperature"

def func(capability: Capability | str):
    if capability in (Capability.COLOR_CONTROL, Capability.COLOR_TEMPERATURE):
        print(capability, type(capability))

func(Capability.COLOR_CONTROL)
func("colorControl")
colorControl <enum 'Capability'>
colorControl <class 'str'>

I have tests for this fix in my stack, but it looks like I split the tests for this change into a different commit

Ideally we could narrow to a literal, but in general mypy is missing a bunch of logic around the values of enums

@hauntsaninja
Copy link
Collaborator Author

#20672 includes more tests here

@cdce8p
Copy link
Collaborator

cdce8p commented Jan 28, 2026

Thanks for the post, but this is actually an improvement!

Good point, missed that! Thanks for working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants