Skip to content

[Typing] Exports of Cython objects are treated as Any #293

@eddiebergman

Description

@eddiebergman

I'm trying to have some type safety which involves having a variable known to be a type of ConfigurationSpace, however mypy fails completely at this and downcasts it to simply be Any.

Screenshot_2023-01-26_13-11-08

Screenshot_2023-01-26_13-11-20

My guess is that mypy relies on reading the source files of a library to infer types and does not read .pyx files.

Two possible solutions if we want to support this, while keeping Cython code:

  1. We move all .pyx functionality to standalone functions and just call them from plain python functions. My estimate from looking at call traces and generated code from previous work is that this interplay between python calling a Cythonized function is relatively low, especially if the types passed beetween the two layers are simple/array types. However this overhead could be quite high if done constantly, which should be avoided.
  2. Release .pyi files, essentially typestubs. This would however add even more maintanence issues as these .pyi files would need to be kept in sync and be correct as they become the source of truth for code linters and mypy, disregarding implementation of the .pyx files. There exists non-perfect automated solutions for regular .py files (e.g. pyright can generate them) but a quick google found nothing for cython code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions