Skip to content

Admit that **kwargs mapping subtypes may have no direct type parameters #18850

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

Merged

Conversation

sterliakov
Copy link
Collaborator

Fixes #13675. I don't know why this check was ever needed (since #11151), but it doesn't seem correct.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks! Not sure what this was about (although maybe primer will tell us)

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ src/hydra_zen/structured_configs/_implementations.py:2950: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "str"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:2950: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "tuple[type, ...]"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:2950: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "dict[str, Any] | None"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:2950: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "bool"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:2950: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "str | None"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:3310: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "str"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:3310: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "tuple[type, ...]"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:3310: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "dict[str, Any] | None"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:3310: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "bool"  [arg-type]
+ src/hydra_zen/structured_configs/_implementations.py:3310: error: Argument 2 to "make_dataclass" has incompatible type "**StrictDataclassOptions"; expected "str | None"  [arg-type]

@sterliakov
Copy link
Collaborator Author

sterliakov commented Mar 29, 2025

Okay, I'm lost in that 1000+ LOC method, looks quite scary, but fortunately it isn't relevant. The actual bug is here, attempt to overwrite a key with itself but Required:

https://github.com/mit-ll-responsible-ai/hydra-zen/blob/0ae045b3ae68b3227a3462815014099920e3ed53/src/hydra_zen/typing/_implementations.py#L432

(trivial to fix and worth fixing anyway, last wrapper dict should just go away, and version condition can define StrictDataclassOptions immediately; mypy struggles to track that StrictDataclassOptions is still a TypedDict, type ignore is for Required[] can be only used in a TypedDict definition error)

The error on our end is cryptic as always with ** unpacking, but it in't a problem to solve in this PR.

Min-ish repro:

import sys
from dataclasses import Field, make_dataclass
from typing import Any, ClassVar, Protocol, Required, TypedDict

class DataClass_(Protocol):
    __dataclass_fields__: ClassVar[dict[str, Field[Any]]]


class _BaseOptions(TypedDict, total=False):
    cls_name: str
    namespace: dict[str, Any] | None
    bases: tuple[type[DataClass_], ...]
    init: bool
    repr: bool
    eq: bool
    order: bool
    unsafe_hash: bool
    frozen: bool

class Py310Options(_BaseOptions):
    match_args: bool
    kw_only: bool
    slots: bool

if sys.version_info < (3, 10):
    BaseOptions = _BaseOptions
else:
    BaseOptions = Py310Options

class StrictOptions(BaseOptions):
    cls_name: Required[str]  # type: ignore


def chk(o: StrictOptions) -> None:
    make_dataclass(fields=[], **o)

@sterliakov sterliakov marked this pull request as ready for review March 29, 2025 02:56
@hauntsaninja hauntsaninja merged commit 6badb4a into python:master Mar 29, 2025
18 checks passed
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.

Test testPassingMappingSubclassForKeywordVarArg is wrong
2 participants