Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import Polars type definitions like `JoinStrategy`?

JoinStrategy is an input to join: https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.join.html

My static type checking tool seems to be able to get a hold of JoinStrategy, but I'm not sure how/from where.

enter image description here

Usually, type stub packages are available on PyPI, but nothing obvious stands out in this case: https://pypi.org/user/ritchie46/

How do I import JoinStrategy (or other type definitions provided by Polars) for my own use?

like image 928
bzm3r Avatar asked Sep 01 '25 20:09

bzm3r


1 Answers

Types can be provided by one of two things: a separate type package, and by the same package. In this case it's being provided by the same package.

If you read the source code, you can find where the type comes from.

Example:

from polars._typing import JoinStrategy

Note that _typing denotes that this is part of the Polars private API, and is subject to change between releases.

You can also print out the value of JoinStrategy:

>>> JoinStrategy
typing.Literal['inner', 'left', 'right', 'full', 'semi', 'anti', 'cross', 'outer']

You can also use this as a type definition.

import typing
JoinStrategy = typing.Literal['inner', 'left', 'right', 'full', 'semi', 'anti', 'cross', 'outer']

This has the advantage of not using the private API, but the disadvantage that if Polars adds a new JoinStrategy, your code won't automatically allow that in this type.

like image 190
Nick ODell Avatar answered Sep 07 '25 18:09

Nick ODell