Skip to content
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

Use a dict to keep track of TypedDict fields in semanal #18369

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

Useful for #7435

This comment has been minimized.

Copy link
Collaborator

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Overall I think the change makes sense. Some points

  1. Do we (or plugin authors) at any point need to access the field_name / type by index? I guess one of the reasons for list was that dicts didn't keep the insertion order in the past. It should probably be possible still do index access with list(fields)[i] instead now.
  2. This might be a breaking change for plugin authors. Don't know of any but I remember that I once experimented with a custom plugin using the TypedDictAnalyzer.

IIRC the separate name and type lists are a common pattern in the mypy code base. Especially for function arguments. I did wonder in the past if there is a better way to do it as those are often iterated over together but I guess we probably shouldn't break that much working code unless we have to.

mypy/semanal_typeddict.py Outdated Show resolved Hide resolved
Comment on lines 281 to +283
def analyze_typeddict_classdef_fields(
self, defn: ClassDef, oldfields: list[str] | None = None
) -> tuple[list[str] | None, list[Type], list[Statement], set[str], set[str]]:
self, defn: ClassDef, oldfields: Collection[str] | None = None
) -> tuple[dict[str, Type] | None, list[Statement], set[str], set[str]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The docstring here should be updated.

mypy/semanal_typeddict.py Show resolved Hide resolved
mypy/semanal_typeddict.py Show resolved Hide resolved
@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Jan 1, 2025

I think hopefully not? TypedDictType already uses a dictionary and TypedDictAnalyzer currently complains about duplicate keys. My motivation here is making #7435 easier in some future PR.

I think this is maybe category 2 or 3 of plugin related change. Searching online in Github, I could find just one instance of breakage here https://github.com/linw1995/data_extractor , but I think that one is already not up to date for the introduction of ReadOnly

Copy link
Contributor

github-actions bot commented Jan 1, 2025

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

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