-
Notifications
You must be signed in to change notification settings - Fork 766
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
conint (false?) flagged as a problem in PROBLEMS tab #4675
Comments
You're using a call expression ( If you provide more context about what you're attempting to do, we may be able to give you some additional tips. |
I found this example. looks like |
That example is invalid. Call expression forms are not allowed in type expressions (annotations). |
You say it "isn't permitted", but the code surely runs, and for libraries such as Pydantic, it is very helpful. In what way can it "not be permitted", if it works? It's like saying you can't assign a number to a variable that used to contain a string - surely some people don't like that, but Python will allow it and the code will run. Allowing e.g. |
The purpose of static type checking is to help you find bugs ahead of time and to write robust code that will not break over time as you update libraries, refactor your code, etc. If you find that static type checking is not sufficiently beneficial, you have the option of disabling it (by setting |
I realize that, but I believe the point of functions as types (such as Although these validation operations are wildly different in operation, they conceptually check similar things, and therefore it makes sense (and is quite ergonomic) to a developer to have them in the same place. Interestingly, the definition of the def conint(
*,
strict: bool | None = None,
gt: int | None = None,
ge: int | None = None,
lt: int | None = None,
le: int | None = None,
multiple_of: int | None = None,
) -> type[int]:
"""A wrapper around `int` that allows for additional constraints.
Args:
strict: Whether to validate the integer in strict mode. Defaults to `None`.
gt: The value must be greater than this.
ge: The value must be greater than or equal to this.
lt: The value must be less than this.
le: The value must be less than or equal to this.
multiple_of: The value must be a multiple of this.
Returns:
The wrapped integer type.
"""
return Annotated[ # type: ignore[return-value]
int,
Strict(strict) if strict is not None else None,
annotated_types.Interval(gt=gt, ge=ge, lt=lt, le=le),
annotated_types.MultipleOf(multiple_of) if multiple_of is not None else None,
] My argument with this is that it returns a simple type expression, so why can't a call to a function that returns a type expression be used as a type expression? I realize that I might have to raise this question on the Pyright repository instead, but I feel like the restriction of just saying "you can't have function calls there" seems a bit arbitrary. |
I'm the primary author of pyright, so there's no need to raise the question in the pyright repo. You'll get the same answer there. The restriction on call expressions isn't specific to pyright. It's something that none of the Python type checkers (including mypy, pyre, and pytype) allow. Type expressions need to be fast to evaluate and non-ambiguous. Call expressions are slow to evaluate and can evaluate differently based on subtle differences between type checkers (e.g. due to overload rules). PEP 484, which introduced the original aspects of static typing to Python, indicates that "Annotations should be kept simple or static analysis tools may not be able to interpret the values." This statement is somewhat ambiguous, but it was later clarified and agreed upon by all of the type checker maintainers. Call expressions are not allowed in type expressions. Like I said, if you don't want to use static type checking, you can disable it. If you want to use static type checking, you need to abide by its rules. |
Environment data
Code Snippet
class Rating(BaseModel):
"""
new_rating by userid from a movieid
XXX
Repro Steps
Expected behavior
conint recognised and correctly assessed
XXX
Actual behavior
Flagged as problem in PROBLEMS tab with message
"
Call expression not allowed in type expression Pylance(reportGeneralTypeIssues)
"
and "conint(ge=0,le=5)" as shown in code snippet is underlined in red.
XXX
Logs
The text was updated successfully, but these errors were encountered: