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

Stubtest whines about Concatenate given as Callable argument #12825

Closed
JulienPalard opened this issue May 20, 2022 · 3 comments
Closed

Stubtest whines about Concatenate given as Callable argument #12825

JulienPalard opened this issue May 20, 2022 · 3 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate topic-stubtest

Comments

@JulienPalard
Copy link
Member

With the concatenate example from the typing doc, which works with mypy 0.950, if I split it to a file and a stub, stubtest starts to compain about Concatenate being used as a Callable argument:

totest.py:

from threading import Lock

# Use this lock to ensure that only one thread is executing a function
# at any time.
my_lock = Lock()

def with_lock(f):
    '''A decorator which provides a lock.'''
    def inner(*args, **kwargs):
        # Provide the lock as the first argument.
        return f(my_lock, *args, **kwargs)
    return inner

@with_lock
def sum_threadsafe(lock, numbers):
    '''Add a list of numbers together in a thread-safe manner.'''
    with lock:
        return sum(numbers)

# We don't need to pass in the lock ourselves thanks to the decorator.
sum_threadsafe([1.1, 2.2, 3.3])

totest.pyi:

from threading import Lock
from typing import Concatenate, ParamSpec, TypeVar, Callable

P = ParamSpec('P')
R = TypeVar('R')


def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]: ...

stubtest run:

$ stubtest totest
error: not checking stubs due to mypy build errors:
totest.pyi:8: error: The first argument to Callable must be a list of types or "..."
@JulienPalard JulienPalard added the bug mypy got something wrong label May 20, 2022
@AlexWaygood AlexWaygood added topic-stubtest topic-paramspec PEP 612, ParamSpec, Concatenate labels May 20, 2022
@hauntsaninja
Copy link
Collaborator

Thanks for the report, but I can't repro. Are you sure you're using mypy 0.950? Consider using python -m mypy.stubtest totest as the invocation.

I do get:

error: totest.P is an unknown mypy node
Stub: at line 5
ParamSpecExpr:5()
Runtime:
MISSING

error: totest.my_lock is not present in stub
Stub:
MISSING
Runtime:
<unlocked _thread.lock object at 0x1124fa090>

error: totest.sum_threadsafe is not present in stub
Stub:
MISSING
Runtime: at line 9 in file /Users/shantanu/devoai/openai/totest.py
<function with_lock.<locals>.inner at 0x10e94ce50>

where the first error is something stubtest could improve.

The error you're getting is actually not stubtest specific, it's from the underlying mypy build. mypy itself only gained support for Concatenate in 0.950.

@JulienPalard
Copy link
Member Author

Thanks for trying my reproducer @hauntsaninja. As you, I can't repro today. What I think happened is that I had a mypy 0.950 in the venv (clearly remember mypy --version telling me so) but for reasons bash's hash got me another stubtest (I also have a user install of mypy). TIL about python -m mypy.stubtest, it would have avoided me and you the issue. Sorry for the noise.

Maybe adding a --version to stubtest would be helpfull for those cases.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 22, 2022

That's a good suggestion! And thanks for reporting, despite the lack of repro, a number of things have come from this:

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue May 22, 2022
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue May 22, 2022
hauntsaninja added a commit that referenced this issue May 23, 2022
Suggested in #12825

Co-authored-by: hauntsaninja <>
hauntsaninja added a commit that referenced this issue May 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate topic-stubtest
Projects
None yet
Development

No branches or pull requests

3 participants