Skip to content

dict ** unpacking does not respect descriptor #2751

Closed

Description

@grievejia

Describe the Bug

Minimal repro:

from typing import Any

class Column[T]:
    """Simplified SQLAlchemy-like column/attribute descriptor."""

    def __get__(self, obj: Any, objtype: Any = None) -> T: ...
    def __set__(self, obj: Any, value: T) -> None: ...

class CallbackModel:
    data: Column[dict[str, Any]]

def get_metric_tags(model: CallbackModel) -> dict[str, Any]:
    return {"result": "success", **model.data}  # FP: Expected a mapping, got Column[dict]

No other type checker wants to error here

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BBdKQA66MQGMoqOHDoBhXFACuNdAG0AKgF1EYugboiQx4wGUuxKBEgxMdMwEUAMgKgSAFjBqkAtNYBrGDoJJVV0AHpUBgZKCGxlBmDMeAk44gZeQlMTE3F0QzoUsDoAfVK2GAZygAo4GCgwABo6XGwAK0RBYRa29qZiGC6hUjoAXjoAOVx0GABKOl8APjpNLsIN-UNistL66tK6hubWjuGeugA3VBUh1YXlqZm7jez8qRk5eRuobFQJAIAWVwKSgegK22iqC6ihUanUmAgEgY6jgsRaI202jEYh2lWqNCqcQkpQYqDYcBqNBBDRhPz%2BAOBoIeK0RyNR6O6pF0WwMlCqykoBWAxn5cGUUAYxi6xnFEgk8DgxhaACoVdTQYRMFCAL4GADEdAAYgAFLoAUXwg2RdjoqDoNFQxBY7BabFw-Fh4QRSIY2hATRAiWgcBI5EQIENAFUGNAIEw6GBlOhkRAZkr8jswLxHdV0KpsDBKDV8F1WAwWXQ0ZRwYV%2BQxBQUwMZJgWi11gPgdaZ0AGQGR%2BWAoKRCJkaFAKIaTaRB8OqxgcAQQjNIGxBdE0%2Bg3oazDBgh4YsQ4IgIhEBzAhyPeGwIjBIphcBI4BFQuhV%2BvYzMIoneHbrtBUGwWBlzfCA10oDcZlaDJN1DMQyAYDwZl8S4izgTdxiMEAAGZCAARgAJh7EAdUDf5Y1Qo1oBgCg0CwPAiDIEigA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    usabilityUsability & readiness issues identified with running Pyrefly on top OSS projects
    on Mar 10, 2026
  2. grievejia commented on Mar 10, 2026

    @grievejia
    ContributorAuthor

    Thinking twice about this, it seems like the current behavior is somewhat intentional given that CallabckModel.data is not initialized on the class, which in the strict runtime terms shouldn't trigger descriptor protocol.

    That said, ORM libraries like SQLAlchemy likes to do this kind of stuff, and we seem to be the only type checker that currently chooses to strictly adhere to the runtime behaviors. I think we'd want some opinions from @stroxler the descriptor expert regarding what we should do here.

  3. added
    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.
    on Mar 10, 2026
  4. stroxler commented on Mar 12, 2026

    @stroxler
    Contributor

    The behavior is indeed intentional. I think the tricky issue is that as you note, some magic libraries modify behavior, and we don't have an easy way to see this.

    I think the best option would actually be to use an intersection type, if we trust our intersection behaviors enough - that's probably a controversial stance, but the issue is there's no right answer here, just different flavors of sometimes-right and sometimes-wrong.

    The only way to avoid false positives is to unsoundly assume both possible behaviors. This is true of all ambiguous descriptors (this is one case, but callable types have related issues where they are impossible to analyze correctly, you have to make a dubious assumption that is sometimes wrong no matter what you do)

    I feel pretty strongly about this (that intersections are the best answer to un-analyzable cases like this, and that a type checker that uses them will beat every other type checker in usability on relevant codebases), but I may also be the only person who thinks this.

    If we had a tagged intersection with a reason, we could also make the abiguity visible to users, which is helpful since unsoundness means that they will hit runtime type errors if they try to rely on both behaviors. I think we want to avoid red squiggles, but some kind of trace in hover would be pretty helpful. This would also be useful for type-aware linters, which we've always thought is a plausible downstream toolset for Pyrefly

  5. rchen152 commented on Jun 13, 2026

    @rchen152
    Contributor

    Pyrefly no longer errors on this example

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.typecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions