Skip to content

frozendict is invariant (python 3.15) #5289

Todo

Description

@IIIMADDINIII

Describe the Bug

# Mutable list is invariant
list1: list[int] = [10]
list2: list[int | None] = list1 # Error

# Immutable list (tuple) is covariant
tuple1: tuple[int, ...] = (10, )
tuple2: tuple[int | None, ...] = tuple1 # No Error

# Mutable dict is invariant
dict1: dict[str, int] = {"a": 10}
dict2: dict[str, int | None] = dict1 # Error 

# Immutable dict is also invariant????
fdict1: frozendict[str, int] = frozendict(**{"a": 10})
fdict2: frozendict[str, int | None] = fdict1 # Error
# But I think this should not be an error
# I think Key and Value should booth be covariant

Python: 3.15

I think a frozen dict should be covariant the same as a tuple is.
If the datatype is readonly, it should always be save to widen the generic Parameters?
As far as i understand, the bundled typeshed types do not declare the Generic Parameters to be covariant.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.jVVNS8NAEP0rgzk0SsiH4MWL4K2IIgherIdtE5pgm5XupqDif_fN7CbZ1FpMIIHZmXnzdufN_jUyI7rvLA8IURA3VdPu1a5RrV20bCq8trDlr6jspchBji2X44LvWV6XEIpo6JSI5tttCBHbDqo8Z6iVHqDECCz5c86E0jTljHGRJ4SDlhWADh4edXB0KYD9oEP4nl-JsXvAj03A5B_PjgRrQvJrcabQj1Tk384JsBOngLDk6AnTIeMeVG2MHpFv8KDVPDwm72clMJMiRnN8ERSEnZBAlHQkMCjMpR-PIqLbztKcbN20b_w1ZGrdbUpCH9KyItU6QQgD73ZXfcBe0rOMHO-_1NrWHBGcH7-_lRLRozOQbzqEQGLrDjcRpJMy1FNVUW3tu7nOsj5c79ZZ1WalXplsEpERDwW1V43cb9N0pEXZBmmPS_TqXxKdOYXOTgl0xvqEx0nN_QA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. jorenham commented on Oct 19, 2026

    @jorenham
    Collaborator

    This is a how it's defined in typeshed (src), so it's not specific to Pyrefly. It might be worth opening an issue there, because as far as I can tell, there isn't an issue or PR for this yet.

  2. randolf-scholz commented on Oct 21, 2026

    @randolf-scholz
    Contributor
    fdict1: frozendict[str, int] = frozendict(*{"a": 10})

    this should be frozendict({"a": 10}) or frozendict(**{"a": 10}), no? though it seems pyrefly currently errors on these

    https://pyrefly.org/sandbox/?project=v2.w1VkgjN-VWpeCrBQ0qiOUUoExoOCoUEtMGSRJLS0UKWQiiINhDI9mOPwqchLLQcrwAxWZYUAiIAC1IXAAgUYH-mlwGILGM56MXnKwBIrVSGjpKSg2EpfH6Y9vyhdPzVPPyU_uVgfRYe-AigFJZYlZoILQ1TjFPLByaAYaCz2-DQlKj7VIdGpji821UGRCVSBN4IA

  3. shahhamdihassan commented on Oct 25, 2026

    @shahhamdihassan
    Contributor

    Thanks for the report, and thanks @jorenham for pointing at typeshed. The related constructor bug (#4994) is now fixed on main. The invariance here comes from how the bundled typeshed stub declares frozendict's type parameters, so it's a typeshed question rather than a pyrefly one; we'll re-check it after our next stub update.

  4. randolf-scholz commented on Oct 26, 2026

    @randolf-scholz
    Contributor

    @shahhamdihassan It seems though frozendict(*{"a": 10}) still goes through without error, I opened a new issue #5381 for that

  5. shahhamdihassan commented on Oct 27, 2026

    @shahhamdihassan
    Contributor

    We've now updated our bundled typeshed, and it still declares frozendict's value type as invariant, so pyrefly will keep reporting this until typeshed changes it. Opening an issue on typeshed, as @jorenham suggested, is the best way to get that fixed, and pyrefly will pick it up with a later stub update.

  6. rchen152 commented on Nov 5, 2026

    @rchen152
    Contributor

    @IIIMADDINIII Thanks for opening an issue with typeshed. Looks like the stub is now fixed, so this should be fixed the next time we update our bundled typeshed.

  7. kinto0 commented on Nov 25, 2026

    @kinto0
    Contributor

    working on a typeshed upgrade which will fix this

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

Metadata

Metadata

Assignees

Labels

py3.15typeshed

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions