Skip to content

Callable type has no attribute __name__ #2375

@lervag

Description

@lervag

Summary

Consider this short snippet:

from concurrent.futures import Future, ProcessPoolExecutor
from typing import Callable, ParamSpec, TypeVar

P = ParamSpec("P")
T = TypeVar("T")


class ProcessPoolManager:
    def __init__(self, max_workers: int):
        self.max_workers: int = max_workers
        self.__executor = ProcessPoolExecutor(max_workers=self.max_workers)

    def submit_job(self, fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Future[T]:
        future = self._executor.submit(fn, *args, **kwargs)
        future.add_done_callback(lambda f: self._handle_job_completion(f, fn.__name__))

        return future

With pyright/basedpyright, I don't get any type warnings (it seems the assume callable is a named function). But with ty I get the following:

> ty check test.py
error[unresolved-attribute]: Object of type `(**P@submit_job) -> T@submit_job` has no attribute `__name__`
  --> test.py:15:75
   |
13 |     def submit_job(self, fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Future[T]:
14 |         future = self._executor.submit(fn, *args, **kwargs)
15 |         future.add_done_callback(lambda f: self._handle_job_completion(f, fn.__name__))
   |                                                                           ^^^^^^^^^^^
16 |
17 |         return future
   |
help: Function objects have a `__name__` attribute, but not all callable objects are functions
info: rule `unresolved-attribute` is enabled by default

Found 1 diagnostic

The help message is very useful here, and I see that it is probably right. But I can't figure out how to either change my signature to only allow "named" functions or how to adjust so that we only get __name__ if it is available. I was hoping someone here might have an idea?

Version

> ty version
ty 0.0.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    callablesquestionAsking for support or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions