Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Base {} Dict Should Infer from Function Return Type #18700

Open
dibrinsofor opened this issue Feb 18, 2025 · 5 comments
Open

Base {} Dict Should Infer from Function Return Type #18700

dibrinsofor opened this issue Feb 18, 2025 · 5 comments
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations

Comments

@dibrinsofor
Copy link

Bug Report

I think this may be a possible refinement bug. Mypy flags an error on the assignment line, and not the return statement. So it is clear Mypy flags {} as a subtype of {str, str}, but I think this should be one of those cases where having refinements should reduce the annotation burden on developers. Unless we want to throw an error because of imprecision.

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
def func() -> dict[str, str]: 
    x = {} ## error here for annot 
    return x

reproducible link: Gist Playground

Expected Behavior

I would expect Mypy to notice the reveal_type(x) as dict[str, str] based off the functions return type and not raise an error.

Actual Behavior

main.py:2: error: Need type annotation for "x" (hint: "x: dict[<type>, <type>] = ...")  [var-annotated]

Your Environment

  • Mypy version used:
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:
@dibrinsofor dibrinsofor added the bug mypy got something wrong label Feb 18, 2025
@sterliakov sterliakov added the topic-inference When to infer types or require explicit annotations label Feb 18, 2025
@Bellkross
Copy link

Bellkross commented Feb 18, 2025

Hey, is it OK if I contribute to solving this one? Never contributed to open-source before, but I like mypy and would like to try.

@A5rocks
Copy link
Collaborator

A5rocks commented Feb 19, 2025

Sure but I suspect an issue labelled good first issue would be better.

Edit: oops, they're all taken. Looking through recent issues, I would suspect #18692 is an easier fix than this (though I'm not familiar enough with mypy to know for sure without... doing it myself).

@Bellkross
Copy link

Sure but I suspect an issue labelled good first issue would be better.

Thank you, I will try to fix this one then. I will find something easier if I make no progress in a couple of weeks.

Edit: oops, they're all taken. Looking through recent issues, I would suspect #18692 is an easier fix than this (though I'm not familiar enough with mypy to know for sure without... doing it myself).

Yeah, since all “good first issue”s are taken I decided to try this one. And I like the issue because I experienced it myself.

@dibrinsofor
Copy link
Author

dibrinsofor commented Feb 19, 2025

I think I can take this one up this weekend. Happy to pair with @Bellkross

@Bellkross
Copy link

I think I can take this one up this weekend. Happy to pair with @Bellkross

Would be happy to pair up @dibrinsofor

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations
Projects
None yet
Development

No branches or pull requests

4 participants