Skip to content

bug when inferring TypedDicts in method parameters #6522

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

Closed
CesarLanderos opened this issue Mar 7, 2019 · 4 comments
Closed

bug when inferring TypedDicts in method parameters #6522

CesarLanderos opened this issue Mar 7, 2019 · 4 comments

Comments

@CesarLanderos
Copy link

CesarLanderos commented Mar 7, 2019

not sure if this is a bug or i could do something to fix the error mypy raises.

  • Are you reporting a bug, or opening a feature request?
    bug
  • Please insert below the code you are checking with mypy
class AlphaDict(TypedDict):
    kappa: int


def alpha(beta: AlphaDict) -> None:
    pass


class GamaDict(TypedDict, total=False):
    kappa: int


def gama(delta: GamaDict) -> None:
    pass


class EpsilonMutationDict(TypedDict):
    action: str
    kappa: int


def epsilon_resolver(theta: List[EpsilonMutationDict]) -> None:
    for zeta in theta:
        if zeta["action"] == "eta":
            alpha(zeta)
        elif zeta["action"] == "iota":
            gama(zeta)
  • What is the actual behavior/output?
    i get this error:

    error: Argument 1 to "gama" has incompatible type "EpsilonMutationDict"; expected "GamaDict"

  • What is the behavior/output you expect?
    I expect that mypy infers that the dict has the same kappa key in the dict, even tho it is an optional in GamaDict and in EpsilonMutationDict is not

  • What are the versions of mypy and Python you are using?
    python: 3.6.7
    mypy: 0.670

  • Do you see the same issue after installing mypy from Git master?
    have not tried that

  • What are the mypy flags you are using? (For example --strict-optional)

python_version = 3.6
ignore_missing_imports = True
disallow_untyped_defs = True
show_column_numbers = True
warn_redundant_casts = True
disallow_any_generics = True
@CesarLanderos
Copy link
Author

might be similar to #6462, not sure

@ilevkivskyi
Copy link
Member

I think mypy is correct here, your function can be like this:

def gama(delta: GamaDict) -> None:
    del delta['kappa']

which is OK, because that key is optional in GamaDict, but if you pass EpsilonMutationDict it can kill a required key, so mypy prohibits this.

@JukkaL What do you think? Should we just close this?

@CesarLanderos
Copy link
Author

That does makes sense.

Btw, this was very difficult to debug, I didn’t realize the error was caused because some keys were optional in one dict and requiered in the other until i did way too many tests, don’t know how hard it to catch this scenario tho, other cases are well explained, like, when you are missing keys.

@ilevkivskyi
Copy link
Member

It is unfortunately hard to give a more detailed error messages in all cases. We make some efforts for protocols and for typed dicts (since they are both structural), but we don't cover all scenarios. If you have some ideas for better error messages, you can open a separate issue.

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

No branches or pull requests

2 participants