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

Added check for the use of an ellipsis for a default argument value f… #6015

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/analyzer/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export function getFunctionFlagsFromDecorators(evaluator: TypeEvaluator, node: F
flags |= FunctionTypeFlags.Overridden;
} else if (decoratorType.details.builtInName === 'type_check_only') {
flags |= FunctionTypeFlags.TypeCheckOnly;
} else if (decoratorType.details.builtInName === 'overload') {
flags |= FunctionTypeFlags.Overloaded;
}
} else if (isInstantiableClass(decoratorType)) {
if (ClassType.isBuiltIn(decoratorType, 'staticmethod')) {
Expand Down
13 changes: 12 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17278,9 +17278,20 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions

let defaultValueType: Type | undefined;
if (param.defaultValue) {
// If this is a stub file, a protocol, an overload, or a class
// whose body is a placeholder implementation, treat a "...", as
// an "Any" value.
let treatEllipsisAsAny = fileInfo.isStubFile || ParseTreeUtils.isSuiteEmpty(node.suite);
if (containingClassType && ClassType.isProtocolClass(containingClassType)) {
treatEllipsisAsAny = true;
}
if (FunctionType.isOverloaded(functionType) || FunctionType.isAbstractMethod(functionType)) {
treatEllipsisAsAny = true;
}

defaultValueType = getTypeOfExpression(
param.defaultValue,
EvaluatorFlags.ConvertEllipsisToAny,
treatEllipsisAsAny ? EvaluatorFlags.ConvertEllipsisToAny : EvaluatorFlags.None,
makeInferenceContext(annotatedType)
).type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def constrained(first: S, second: S) -> S:


class Foo(Generic[U]):
def generic_func1(self, a: U, b: U = ..., **kwargs: U) -> U:
return b
def generic_func1(self, a: U, b: str = "", **kwargs: U) -> U:
return a


foo = Foo[str]()
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/overload12.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def overload9(x: object, y: int, z: str, a: None) -> str:
...


def overload9(x, y, z=..., a=...) -> Any:
def overload9(x, y, z="", a=None) -> Any:
pass


Expand Down
Loading