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

Regression for type variables bound to literals #4457

Closed
RobertCraigie opened this issue Jan 14, 2023 · 5 comments
Closed

Regression for type variables bound to literals #4457

RobertCraigie opened this issue Jan 14, 2023 · 5 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@RobertCraigie
Copy link
Contributor

Describe the bug

This snippet worked prior to 1.1.289

from __future__ import annotations

from typing import Literal, TypeVar, Mapping

_T = TypeVar('_T', bound=Literal['country', 'city'])

def foo(by: list[_T], order: Mapping[_T, str] | None = None) -> None:
    ...

foo(['country'])
foo(['country'], order={'country': 'asc'})

Pyright seems to infer the by and order arguments to list[str] and dict[str, str] which is not correct.

foo.py
 foo.py:13:6 - error: Argument of type "list[str]" cannot be assigned to parameter "by" of type "list[_T@foo]" in function "foo"
    Type "Literal['country']" cannot be assigned to type "_T@foo" (reportGeneralTypeIssues)
  foo.py:14:6 - error: Argument of type "list[str]" cannot be assigned to parameter "by" of type "list[_T@foo]" in function "foo"
    Type "Literal['country']" cannot be assigned to type "_T@foo" (reportGeneralTypeIssues)
  foo.py:14:24 - error: Argument of type "dict[str, str]" cannot be assigned to parameter "order" of type "Mapping[_T@foo, str] | None" in function "foo"
    Type "dict[str, str]" cannot be assigned to type "Mapping[_T@foo, str] | None"
      "dict[str, str]" is incompatible with "Mapping[_T@foo, str]"
        TypeVar "_KT@Mapping" is invariant
          Type "str" cannot be assigned to type "_T@foo"
      Type cannot be assigned to type "None" (reportGeneralTypeIssues)
3 errors, 0 warnings, 0 informations

Expected behavior

The given snippet should pass type checks.

VS Code extension or command-line

Command-line v1.1.289

@erictraut
Copy link
Collaborator

This was an intentional change, not a regression. It was needed to fix another issue in the constraint solver.

Mypy also doesn't accept the above code, so I thought it was unlikely that anyone was relying on the need to bind a type variable to a literal type.

Can you explain the scenario where a type variable bound to a literal (or a union of literals) is useful?

@RobertCraigie
Copy link
Contributor Author

In my case it's used to represent the relationships between the arguments, e.g. you cannot specify a field in order that you haven't included in by.

results = await Profile.prisma().group_by(
    ['country'],
    order={
        # city has to be included in the `by` argument to be valid
        'city': 'asc',
    }
)

erictraut pushed a commit that referenced this issue Jan 15, 2023
…expected type includes a TypeVar that is bound to a type that influences the type inference. This addresses #4457.
@erictraut
Copy link
Collaborator

This will be addressed in the next release.

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Jan 15, 2023
@RobertCraigie
Copy link
Contributor Author

Amazing, thank you so much!

@erictraut
Copy link
Collaborator

This is included in pyright 1.1.290, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants