Skip to content

Conversation

@thejchap
Copy link
Contributor

@thejchap thejchap commented Sep 9, 2025

summary

  • this pr implements the following attributes for Enum members:
    • name
    • _name_
    • value
    • _value_
  • adds a TODO test for my_enum_class_instance.name
  • only implements if the instance is a subclass of Enum re: this comment and existing test

pointers

test plan

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Sep 9, 2025
@thejchap thejchap force-pushed the thejchap/enum-members branch from 4606136 to 64c8a66 Compare September 10, 2025 01:42
@github-actions
Copy link
Contributor

github-actions bot commented Sep 10, 2025

Diagnostic diff on typing conformance tests

Changes were detected when running ty on typing conformance tests
--- old-output.txt	2025-09-17 07:23:56.516264569 +0000
+++ new-output.txt	2025-09-17 07:23:59.584286258 +0000
@@ -316,16 +316,9 @@
 enums_behaviors.py:32:1: error[type-assertion-failure] Argument does not have asserted type `Literal[Color.BLUE]`
 enums_behaviors.py:44:21: error[subclass-of-final-class] Class `ExtendedShape` cannot inherit from final class `Shape`
 enums_expansion.py:52:9: error[type-assertion-failure] Argument does not have asserted type `CustomFlags`
-enums_member_names.py:21:1: error[type-assertion-failure] Argument does not have asserted type `Literal["RED"]`
-enums_member_names.py:22:1: error[type-assertion-failure] Argument does not have asserted type `Literal["RED"]`
-enums_member_names.py:26:5: error[type-assertion-failure] Argument does not have asserted type `Literal["RED", "BLUE"]`
 enums_member_names.py:30:5: error[type-assertion-failure] Argument does not have asserted type `Literal["RED", "BLUE", "GREEN"]`
-enums_member_values.py:21:1: error[type-assertion-failure] Argument does not have asserted type `Literal[1]`
-enums_member_values.py:22:1: error[type-assertion-failure] Argument does not have asserted type `Literal[1]`
-enums_member_values.py:26:5: error[type-assertion-failure] Argument does not have asserted type `Literal[1, 3]`
 enums_member_values.py:30:5: error[type-assertion-failure] Argument does not have asserted type `Literal[1, 2, 3]`
 enums_member_values.py:54:1: error[type-assertion-failure] Argument does not have asserted type `Literal[1]`
-enums_member_values.py:68:1: error[type-assertion-failure] Argument does not have asserted type `Literal[1]`
 enums_member_values.py:96:1: error[type-assertion-failure] Argument does not have asserted type `int`
 enums_members.py:128:21: info[revealed-type] Revealed type: `Unknown | Literal[2]`
 enums_members.py:129:9: error[type-assertion-failure] Argument does not have asserted type `Unknown`
@@ -869,5 +862,5 @@
 typeddicts_usage.py:28:1: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
 typeddicts_usage.py:28:18: error[invalid-key] Invalid key access on TypedDict `Movie`: Unknown key "title"
 typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions. Did you mean to use a concrete TypedDict or `collections.abc.Mapping[str, object]` instead?
-Found 870 diagnostics
+Found 863 diagnostics
 WARN A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 10, 2025

mypy_primer results

Changes were detected when running on open source projects
django-stubs (https://github.com/typeddjango/django-stubs)
- tests/assert_type/db/models/_enums.py:20:1: error[type-assertion-failure] Argument does not have asserted type `Literal["NORTH"]`
- tests/assert_type/db/models/test_enums.py:144:1: error[type-assertion-failure] Argument does not have asserted type `Literal["CLUB"]`
- tests/assert_type/db/models/test_enums.py:155:1: error[type-assertion-failure] Argument does not have asserted type `Literal["SENIOR"]`
- tests/assert_type/db/models/test_enums.py:167:1: error[type-assertion-failure] Argument does not have asserted type `Literal["CAR"]`
- tests/assert_type/db/models/test_enums.py:181:1: error[type-assertion-failure] Argument does not have asserted type `Literal["MALE"]`
- tests/assert_type/db/models/test_enums.py:194:1: error[type-assertion-failure] Argument does not have asserted type `Literal["GOLD"]`
- tests/assert_type/db/models/test_enums.py:207:1: error[type-assertion-failure] Argument does not have asserted type `Literal["FS"]`
- tests/assert_type/db/models/test_enums.py:221:1: error[type-assertion-failure] Argument does not have asserted type `Literal["PI"]`
- tests/assert_type/db/models/test_enums.py:235:1: error[type-assertion-failure] Argument does not have asserted type `Literal["ABYSS"]`
+ tests/assert_type/db/models/test_enums.py:237:1: error[type-assertion-failure] Argument does not have asserted type `Any`
- tests/assert_type/db/models/test_enums.py:248:1: error[type-assertion-failure] Argument does not have asserted type `Literal["NORTH"]`
- tests/assert_type/db/models/test_enums.py:260:1: error[type-assertion-failure] Argument does not have asserted type `Literal["NORTH"]`
- Found 476 diagnostics
+ Found 466 diagnostics

discord.py (https://github.com/Rapptz/discord.py)
- discord/audit_logs.py:550:56: error[invalid-argument-type] Argument to bound method `from_data` is incorrect: Expected `int`, found `None | Unknown | (Unknown & ~None) | Literal[-1]`
+ discord/audit_logs.py:550:56: error[invalid-argument-type] Argument to bound method `from_data` is incorrect: Expected `int`, found `None | Unknown | (Unknown & ~None) | Literal[3, 4, 1, 5, -1]`
- discord/audit_logs.py:551:55: error[invalid-argument-type] Argument to bound method `from_data` is incorrect: Expected `int`, found `None | Unknown | (Unknown & ~None) | Literal[-1]`
+ discord/audit_logs.py:551:55: error[invalid-argument-type] Argument to bound method `from_data` is incorrect: Expected `int`, found `None | Unknown | (Unknown & ~None) | Literal[3, 4, 1, 5, -1]`
No memory usage changes detected ✅

@thejchap
Copy link
Contributor Author

@sharkdp could use some feedback on approach for _value_/value before i get further in

i am thinking to add a value_ty field to EnumLiteralType that stores this value, which i think would require giving EnumMetadata the 'db lifetime

anything im missing there or other recommendations?

@sharkdp
Copy link
Contributor

sharkdp commented Sep 12, 2025

i am thinking to add a value_ty field to EnumLiteralType that stores this value

I would rather expect that we add the value Type for enum members to members on EnumMetadata? Maybe by turning it to a Name => Type HashMap? Is that what you meant?

which i think would require giving EnumMetadata the 'db lifetime

Yes, it looks like that would require adding a lifetime parameter to EnumMetadata.

anything im missing there

I don't think so

@thejchap thejchap force-pushed the thejchap/enum-members branch from 64c8a66 to da7ed1f Compare September 13, 2025 03:03
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 13, 2025

CodSpeed Instrumentation Performance Report

Merging #20311 will not alter performance

Comparing thejchap:thejchap/enum-members (78528f6) with main (d121a76)

Summary

✅ 43 untouched

@thejchap thejchap force-pushed the thejchap/enum-members branch 2 times, most recently from 5c75f79 to 1c247f9 Compare September 13, 2025 12:33
@thejchap thejchap force-pushed the thejchap/enum-members branch 2 times, most recently from 64830e6 to 809a225 Compare September 13, 2025 12:48
@github-actions
Copy link
Contributor

github-actions bot commented Sep 13, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@thejchap thejchap changed the title [ty] infer type for enum members [ty] infer name and value for enum members Sep 13, 2025
@thejchap thejchap force-pushed the thejchap/enum-members branch 4 times, most recently from c34449e to a3af30d Compare September 14, 2025 03:21
@thejchap thejchap marked this pull request as ready for review September 14, 2025 03:30
@github-actions
Copy link
Contributor

ecosystem-analyzer results

Lint rule Added Removed Changed
type-assertion-failure 1 11 0
invalid-argument-type 0 0 2
Total 1 11 2

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much. This looks great.

@thejchap thejchap force-pushed the thejchap/enum-members branch 2 times, most recently from 0370e59 to 0df220f Compare September 16, 2025 18:20
@carljm carljm removed their request for review September 16, 2025 18:37
@thejchap thejchap force-pushed the thejchap/enum-members branch from 0df220f to 63fd2f4 Compare September 16, 2025 20:09
@dcreager dcreager removed their request for review September 17, 2025 01:27
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Glad we could get rid of the performance regression.

@sharkdp sharkdp merged commit 9f0b942 into astral-sh:main Sep 17, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants