python / python/mypy

reveal_type shows the same, but one of the objects generates an error

Open
#15,154 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

How come mypy warns that astype doesn't expect the variable dtypes, while it is OK to explicitly type it there?

from typing import Dict, Union
import pandas as pd

dat = pd.DataFrame(data={"A": [1], "B": [4]})

dtypes = {"A":float, "B":"int64"}
dat[[*dtypes]].astype(dtypes)  # mypy error
dat[[*dtypes]].astype({"A":float, "B":"int64"})  # no mypy error

def myf()->Dict[str, Union[str,type]]:  # this is fine
    return {"A":float, "B":"int64"}
dat[[*dtypes]].astype(myf())    # this is also fine

by calling from typing_extensions import reveal_type, it shows that both the var and the explicit definition has builtins.dict[builtins.str, builtins.object]
While the function return's type is "builtins.dict[builtins.str, Union[builtins.str, builtins.type]]"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python reproducer and compare the inferred types for the variable, the inline dictionary, and the function return at the pandas astype call. Determine whether the differing diagnostics are intended; done means explaining the cause or resolving the inconsistent diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.