Skip to content
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

Error when trying to construct TypedDict with ** unpacking #8883

Closed
Dreamsorcerer opened this issue May 25, 2020 · 1 comment
Closed

Error when trying to construct TypedDict with ** unpacking #8883

Dreamsorcerer opened this issue May 25, 2020 · 1 comment

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented May 25, 2020

Am I asking too much of Mypy, or am I doing something wrong with this code?

I get Incompatible types in assignment (expression has type "Dict[str, str]", variable has type "Union[_TokenParamsPassword, _TokenParamsRefresh]") [assignment] on the assignment in this function:

    async def _get_token(self, data: AuthParams) -> TokenResponse:
        request_data: TokenParams = {
            'client_id': OAUTH_CLIENT_ID,
            'client_secret': OAUTH_CLIENT_SECRET,
            **data
        }

The type definitions are:

class _AuthParamsPassword(TypedDict):
    grant_type: Literal["password"]
    email: str
    password: str
class _AuthParamsRefresh(TypedDict):
    grant_type: Literal["refresh_token"]
    refresh_token: str
AuthParams = Union[_AuthParamsPassword, _AuthParamsRefresh]

class _TokenParamsPassword(TypedDict):
    grant_type: Literal["password"]
    client_id: str
    client_secret: str
    email: str
    password: str
class _TokenParamsRefresh(TypedDict):
    grant_type: Literal["refresh_token"]
    client_id: str
    client_secret: str
    refresh_token: str
TokenParams = Union[_TokenParamsPassword, _TokenParamsRefresh]

So, I'm using the AuthParam, which is a subset of keys of TokenParam, and then building a full TokenParam dictionary from that. Is there a better way to do this?

@JelleZijlstra JelleZijlstra changed the title Incompatible type in assignment when trying to construct TypedDict. Error when trying to construct TypedDict with ** unpacking May 3, 2022
@AlexWaygood
Copy link
Member

Closing as a duplicate of #9408, which is a more recent issue but has a simpler repro

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

No branches or pull requests

3 participants