from __future__ import annotations
import inspect
import os
import re
import sys
import warnings
from pathlib import Path
from typing import Any
import sphinx_autosummary_accessors
sys.path.insert(0, str(Path("../..").resolve()))
project = "Polars"
author = "Ritchie Vink"
copyright = f"2025, {author}"
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.githubpages",
"sphinx.ext.intersphinx",
"sphinx.ext.linkcode",
"sphinx.ext.mathjax",
"autodocsumm",
"numpydoc",
"sphinx_autosummary_accessors",
"sphinx_copybutton",
"sphinx_design",
"sphinx_favicon",
"sphinx_reredirects",
"sphinx_toolbox.more_autodoc.overloads",
]
default_role = "code"
maximum_signature_line_length = 88
templates_path = ["_templates", sphinx_autosummary_accessors.templates_path]
exclude_patterns = ["Thumbs.db", ".DS_Store"]
overloads_location = ["bottom"]
intersphinx_mapping = {
"numpy": ("https://numpy.org/doc/stable/", None),
"pandas": ("https://pandas.pydata.org/docs/", None),
"pyarrow": ("https://arrow.apache.org/docs/", None),
"python": ("https://docs.python.org/3", None),
}
numpydoc_show_class_members = False
copybutton_prompt_text = r">>> |\.\.\. "
copybutton_prompt_is_regexp = True
redirects = {"index": "reference/index.html"}
html_theme = "pydata_sphinx_theme"
html_static_path = ["_static"]
html_css_files = ["css/custom.css"]
html_show_sourcelink = False
static_assets_root = "https://raw.githubusercontent.com/pola-rs/polars-static/master"
github_root = "https://github.com/pola-rs/polars"
web_root = "https://docs.pola.rs"
git_ref = os.environ.get("POLARS_VERSION", "main")
version_match = re.fullmatch(r"py-(\d+)\.\d+\.\d+.*", git_ref)
switcher_version = version_match.group(1) if version_match is not None else "dev"
html_js_files = [
(
"https://plausible.io/js/script.js",
{"data-domain": "docs.pola.rs,combined.pola.rs", "defer": "defer"},
),
]
html_theme_options = {
"external_links": [
{
"name": "User guide",
"url": f"{web_root}/",
},
{
"name": "Polars Cloud API reference",
"url": "https://docs.cloud.pola.rs/reference/index.html",
},
],
"icon_links": [
{
"name": "GitHub",
"url": github_root,
"icon": "fa-brands fa-github",
},
{
"name": "Discord",
"url": "https://discord.gg/4UfP5cfBE7",
"icon": "fa-brands fa-discord",
},
{
"name": "X/Twitter",
"url": "https://x.com/datapolars",
"icon": "fa-brands fa-x-twitter",
},
{
"name": "Bluesky",
"url": "https://bsky.app/profile/pola.rs",
"icon": "fa-brands fa-bluesky",
},
],
"logo": {
"image_light": f"{static_assets_root}/logos/polars-logo-dark-medium.png",
"image_dark": f"{static_assets_root}/logos/polars-logo-dimmed-medium.png",
},
"switcher": {
"json_url": f"{web_root}/api/python/dev/_static/version_switcher.json",
"version_match": switcher_version,
},
"show_version_warning_banner": False,
"navbar_end": ["theme-switcher", "version-switcher", "navbar-icon-links"],
"check_switcher": False,
}
favicons = [
{
"rel": "icon",
"sizes": "32x32",
"href": f"{static_assets_root}/icons/favicon-32x32.png",
},
{
"rel": "apple-touch-icon",
"sizes": "180x180",
"href": f"{static_assets_root}/icons/touchicon-180x180.png",
},
]
def linkcode_resolve(domain: str, info: dict[str, Any]) -> str | None:
"""
Determine the URL corresponding to Python object.
Based on pandas equivalent:
https://github.com/pandas-dev/pandas/blob/main/doc/source/conf.py#L629-L686
"""
if domain != "py":
return None
modname = info["module"]
fullname = info["fullname"]
submod = sys.modules.get(modname)
if submod is None:
return None
obj = submod
for part in fullname.split("."):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
obj = getattr(obj, part)
except AttributeError:
return None
try:
fn = inspect.getsourcefile(inspect.unwrap(obj))
except TypeError:
try:
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
except (AttributeError, TypeError):
fn = None
if not fn:
return None
try:
source, lineno = inspect.getsourcelines(obj)
except TypeError:
try:
source, lineno = inspect.getsourcelines(obj.fget)
except (AttributeError, TypeError):
lineno = None
except OSError:
lineno = None
linespec = f"#L{lineno}-L{lineno + len(source) - 1}" if lineno else ""
conf_dir_path = Path(__file__).absolute().parent
polars_root = (conf_dir_path.parent.parent / "polars").absolute()
fn = os.path.relpath(fn, start=polars_root)
return f"{github_root}/blob/{git_ref}/py-polars/polars/{fn}{linespec}"
def _minify_classpaths(s: str) -> str:
s = s.replace("datetime.", "")
return re.sub(
pattern=r"""
~?
(
(?:pl|
(?:polars\.
(?:_reexport|datatypes)
)
)
(?:\.[a-z.]+)?\.
([A-Z][\w.]+)
)
""",
repl=r"\2",
string=s,
flags=re.VERBOSE,
)
def process_signature(
app: object,
what: object,
name: object,
obj: object,
opts: object,
sig: str,
ret: str,
) -> tuple[str, str]:
return (
_minify_classpaths(sig) if sig else sig,
_minify_classpaths(ret) if ret else ret,
)
def setup(app: Any) -> None:
app.connect("autodoc-process-signature", process_signature)