Skip to content

The ellipsis in the function parameters of challenge intermediate/literalstring causes challenge failed #81

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

Closed
b1indsight opened this issue Dec 29, 2023 · 3 comments

Comments

@b1indsight
Copy link

In challenge intermediate/literalstring, function execute_query has a parament accepted Iterable[str],and set default value .... The same pattern is observed in the provided solution.

def execute_query(sql, parameters: Iterable[str] = ...):
    """No need to implement it"""

This will cause a type check error by pyright

error: Expression of type "ellipsis" cannot be assigned to parameter of type "Iterable[str]"

It seems like I must access type check by changing Iterable[str] to Iterable[str] | EllipsisType , like

from typing import Iterable, LiteralString
from types import EllipsisType


def execute_query(sql: LiteralString, parameters: Iterable[str] | EllipsisType = ...):
    """No need to implement it"""

or change ... to () for type check.

from typing import Iterable, LiteralString
from types import EllipsisType


def execute_query(sql: LiteralString, parameters: Iterable[str] = ()):
    """No need to implement it"""

I don't think either of the above solutions to be the best way for this issue, but I don't know any better way to use ellipsis for default parameter with type hint.

@laike9m
Copy link
Owner

laike9m commented Dec 31, 2023

May I ask which type checker are you using? Python-Type-Challenges uses pyright and it works as expected, see playground. It is normal that different type checkers behave differently here and there.

parameters: Iterable[str] = () works too, if you make a PR I'll accept it.

@b1indsight
Copy link
Author

I'm using Pyright version 1.1.344, which you can reproduce in this playground.

I found a related discussion in this issue of pyright and the corresponding release note of version 1.1.329. The key change mentioned is:

Behavior Change: Added check for the use of an ellipsis for a default argument value for a function that is not in a stub, not overloaded, and without a placeholder implementation.


When an ellipsis is used in a function body, means not been implemented, Pyright treats the ellipsis as Any, for example:

from typing import LiteralString, Iterable
def execute_query(sql: LiteralString, parameters: Iterable[str] = ...):
     ...

will get:

> pyright --pythonversion=3.12  test.py
0 errors, 0 warnings, 0 informations 

When using docstring, pass, and NotImplementedError, Pyright will check the type of ellipsis used as a default parameter. This behavior causes the type check error.

from typing import LiteralString, Iterable
def execute_query(sql: LiteralString, parameters: Iterable[str] = ...):
    """Not implemented yet."""
>  pyright --pythonversion=3.12  test.py
test.py:3:67 - error: Expression of type "ellipsis" cannot be assigned to parameter of type "Iterable[str]"
    "ellipsis" is incompatible with protocol "Iterable[str]"
      "__iter__" is not present (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 

I think adding an ellipsis within the function execute_query's body of question file is a better way to fix this. The solution file can pass the type check (I made a mistake for this).

@laike9m
Copy link
Owner

laike9m commented Jan 2, 2024

Thanks for digging into this. You're right about the current behavior (this comment describes it).

I'll make a change to question.py to use ellipsis in function body.

@laike9m laike9m closed this as completed in c6ca3a1 Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants