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

Import Application from panel.io.application, drop 3.9, and ruff #707

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ jobs:
run: |
MATRIX=$(jq -nsc '{
"os": ["ubuntu-latest", "macos-latest", "windows-latest"],
"environment": ["test-39", "test-312"]
"environment": ["test-310", "test-312"]
}')
echo "MATRIX=$MATRIX" >> $GITHUB_ENV
- name: Set test matrix with 'full' option
if: env.MATRIX_OPTION == 'full'
run: |
MATRIX=$(jq -nsc '{
"os": ["ubuntu-latest", "macos-latest", "windows-latest"],
"environment": ["test-39", "test-310", "test-311", "test-312"]
"environment": ["test-310", "test-311", "test-312"]
}')
echo "MATRIX=$MATRIX" >> $GITHUB_ENV
- name: Set test matrix with 'downstream' option
Expand Down
16 changes: 8 additions & 8 deletions doc/generate_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ def extract_type(pobj):
return numbers.Number
elif isinstance(pobj, param.NumericTuple):
if isinstance(pobj.length):
return typing.Tuple[(numbers.Number,)*pobj.length]
return tuple[(numbers.Number,)*pobj.length]
else:
return typing.Tuple[numbers.Number, ...]
return tuple[numbers.Number, ...]
elif isinstance(pobj, param.Tuple):
if isinstance(pobj.length):
return typing.Tuple[(typing.Any,)*pobj.length]
return tuple[(typing.Any,)*pobj.length]
else:
return typing.Tuple[typing.Any, ...]
return tuple[typing.Any, ...]
elif isinstance(pobj, param.List):
item_type = pobj.item_type or pobj.class_
if item_type is None:
return typing.List[typing.Any]
return list[typing.Any]
if isinstance(item_type, tuple):
item_type = typing.Union[item_type]
return typing.List[item_type]
return list[item_type]
elif isinstance(pobj, param.Dict):
return typing.Dict[typing.Hashable, typing.Any]
return dict[typing.Hashable, typing.Any]
elif isinstance(pobj, param.Array):
return np.ndarray
elif isinstance(pobj, param.DataFrame):
Expand Down Expand Up @@ -280,7 +280,7 @@ def write_index(bases):
page += f'## [`{base.__name__}`]({base.__name__})\n\n'
if description:
page += f'{description}\n\n'
path = REFERENCE_PATH / f'index.md'
path = REFERENCE_PATH / 'index.md'
with open(path, 'w') as f:
f.write(page)

Expand Down
4 changes: 2 additions & 2 deletions lumen/ai/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import textwrap

from typing import Literal, Optional
from typing import Literal

import pandas as pd
import panel as pn
Expand Down Expand Up @@ -814,7 +814,7 @@ def _create_transform_model(transforms):
default=True,
),
),
transforms=(Optional[list[Literal[tuple(transforms)]]], None),
transforms=(list[Literal[tuple(transforms)]] | None, None),
)
return transform_model

Expand Down
2 changes: 1 addition & 1 deletion lumen/ai/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ async def _get_agent(self, messages: list | str):
return selected

def _serialize(self, obj, exclude_passwords=True):
if isinstance(obj, (Tabs, Column)):
if isinstance(obj, Tabs | Column):
for o in obj:
if isinstance(obj, ChatStep) and not obj.title.startswith("Selected"):
# only want the chain of thoughts from the selected agent
Expand Down
23 changes: 12 additions & 11 deletions lumen/ai/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import inspect
import warnings

from collections.abc import Callable
from typing import (
Any, Callable, Literal, Optional, TypeVar, Union,
Any, Literal, TypeVar, Union,
)

import param
Expand All @@ -12,7 +13,7 @@
from pydantic.color import Color
from pydantic.fields import FieldInfo, PydanticUndefined

DATE_TYPE = Union[datetime.datetime, datetime.date]
DATE_TYPE = datetime.datetime | datetime.date
PARAM_TYPE_MAPPING: dict[param.Parameter, type] = {
param.String: str,
param.Integer: int,
Expand All @@ -39,7 +40,7 @@ class Config(BaseConfig):
arbitrary_types_allowed = True


def _create_literal(obj: list[Union[str, type]]) -> type:
def _create_literal(obj: list[str | type]) -> type:
"""
Create a literal type from a list of objects.
"""
Expand Down Expand Up @@ -85,7 +86,7 @@ def parameter_to_field(
elif param_type is param.ClassSelector:
type_ = _get_model(parameter.class_, created_models)
if isinstance(type_, tuple):
type_ = Union[tuple([PARAM_TYPE_MAPPING.get(t, t) for t in type_])]
type_ = Union[tuple([PARAM_TYPE_MAPPING.get(t, t) for t in type_])] # noqa
if parameter.default is not None:
default_factory = parameter.default
if not callable(default_factory):
Expand Down Expand Up @@ -118,9 +119,9 @@ def parameter_to_field(
elif parameter.name == "align":
type_ = _create_literal(["auto", "start", "center", "end"])
elif parameter.name == "aspect_ratio":
type_ = Union[Literal["auto"], float]
type_ = Literal["auto"] | float
elif parameter.name == "margin":
type_ = Union[float, tuple[float, float], tuple[float, float, float, float]]
type_ = float | tuple[float, float] | tuple[float, float, float, float]
else:
raise NotImplementedError(
f"Parameter {parameter.name!r} of {param_type.__name__!r} not supported"
Expand All @@ -134,18 +135,18 @@ def parameter_to_field(
pass

if parameter.allow_None:
type_ = Optional[type_]
type_ = type_ | None

return type_, field_info


def param_to_pydantic(
parameterized: type[param.Parameterized],
base_model: type[BaseModel] = ArbitraryTypesModel,
created_models: Optional[dict[str, BaseModel]] = None,
schema: Optional[dict[str, Any]] = None,
excluded: Union[str, list[str]] = "_internal_params",
extra_fields: Optional[dict[str, tuple[type, FieldInfo]]] = None,
created_models: dict[str, BaseModel] | None = None,
schema: dict[str, Any] | None = None,
excluded: str | list[str] = "_internal_params",
extra_fields: dict[str, tuple[type, FieldInfo]] | None = None,
) -> dict[str, BaseModel]:
"""
Translate a param Parameterized to a Pydantic BaseModel.
Expand Down
5 changes: 2 additions & 3 deletions lumen/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import warnings

from functools import partial
from typing import Any, ClassVar
from typing import Any, ClassVar, Literal

import pandas as pd
import panel as pn
import param # type: ignore

from panel.io.cache import _container_hash, _hash_funcs
from panel.util import classproperty
from typing_extensions import Literal

from .state import state
from .util import (
Expand Down Expand Up @@ -82,7 +81,7 @@ def _extract_refs(self, params: dict[str, Any], refs: dict[str, Any]):
for pname, pval in params.items():
if is_ref(pval):
refs[pname] = pval
elif isinstance(pval, (pn.widgets.Widget, param.Parameter, Variable)):
elif isinstance(pval, (pn.widgets.Widget | param.Parameter | Variable)):
var = state.variables.add_variable(pval)
processed[pname] = var.value
refs[pname] = f'$variables.{var.name}'
Expand Down
2 changes: 1 addition & 1 deletion lumen/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from bokeh.document import Document
from panel.command import Serve, main as _pn_main, transform_cmds
from panel.io.server import Application
from panel.io.application import Application
from panel.io.state import set_curdoc

from .. import __version__
Expand Down
2 changes: 1 addition & 1 deletion lumen/command/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from bokeh.application.handlers.code import CodeHandler # type: ignore
from bokeh.command.util import die # type: ignore
from panel.command import Serve, transform_cmds
from panel.io.server import Application
from panel.io.application import Application

from ..config import config

Expand Down
2 changes: 1 addition & 1 deletion lumen/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _validate(self, val):
self._validate_value(val, self.allow_None)

def _validate_value(self, val, allow_None):
if isinstance(val, (dict, str)) or issubclass(val, BasicTemplate):
if isinstance(val, dict | str) or issubclass(val, BasicTemplate):
return
raise ValueError(f"Template type {type(val).__name__} not recognized.")

Expand Down
6 changes: 3 additions & 3 deletions lumen/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import os
import traceback

from collections.abc import Callable
from concurrent.futures import Future, ThreadPoolExecutor
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar,
TYPE_CHECKING, Any, ClassVar, Literal,
)

import panel as pn
Expand All @@ -18,7 +19,6 @@
from panel.io.resources import CSS_URLS
from panel.template.base import BasicTemplate
from panel.viewable import Viewable, Viewer
from typing_extensions import Literal

from .auth import Auth
from .base import Component, MultiTypeComponent
Expand Down Expand Up @@ -887,7 +887,7 @@ def _get_global_filters(self) -> tuple[list[Filter] | None, pn.Column | None]:
continue
for pipeline in layout._pipelines.values():
for filt in pipeline.filters:
if ((all(isinstance(layout, (type(None), Future)) or any(filt in p.filters for p in layout._pipelines.values())
if ((all(isinstance(layout, (type(None) | Future)) or any(filt in p.filters for p in layout._pipelines.values())
for layout in self.layouts) and
filt.panel is not None) and filt.shared) and filt not in filters:
views.append(filt.panel)
Expand Down
3 changes: 2 additions & 1 deletion lumen/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import types

from collections.abc import Callable
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar, Literal,
TYPE_CHECKING, Any, ClassVar, Literal,
)

import bokeh # type: ignore
Expand Down
3 changes: 1 addition & 2 deletions lumen/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from functools import partial
from io import BytesIO, StringIO
from itertools import product
from typing import Any, ClassVar
from typing import Any, ClassVar, Literal

import panel as pn
import param # type: ignore

from panel.util import PARAM_NAME_PATTERN
from panel.viewable import Layoutable, Viewable, Viewer
from param import edit_constant
from typing_extensions import Literal

from .base import Component
from .config import _LAYOUTS
Expand Down
3 changes: 1 addition & 2 deletions lumen/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from functools import partial
from itertools import product
from typing import Any, ClassVar
from typing import Any, ClassVar, Literal

import numpy as np
import panel as pn
Expand All @@ -15,7 +15,6 @@
from panel.io.state import state as pn_state
from panel.viewable import Viewer
from panel.widgets import Widget
from typing_extensions import Literal

from .base import Component
from .filters.base import Filter, ParamFilter, WidgetFilter
Expand Down
8 changes: 4 additions & 4 deletions lumen/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from os.path import basename
from pathlib import Path
from typing import (
TYPE_CHECKING, Any, ClassVar, Literal, Union,
TYPE_CHECKING, Any, ClassVar, Literal,
)
from urllib.parse import quote, urlparse

Expand All @@ -37,8 +37,8 @@
from dask.dataframe import DataFrame as dDataFrame, Series as dSeries
from panel.viewable import Viewable

DataFrame = Union[pd.DataFrame, dDataFrame]
Series = Union[pd.Series, dSeries]
DataFrame = pd.DataFrame | dDataFrame
Series = pd.Series | dSeries

DataFrameTypes: tuple[type, ...]
try:
Expand Down Expand Up @@ -639,7 +639,7 @@ def _named_files(self) -> dict[str, tuple[str, str]]:
tables = self.tables or {}
files = {}
for name, table in tables.items():
if isinstance(table, (list, tuple)):
if isinstance(table, (list | tuple)):
table, ext = table
else:
if isinstance(table, str) and table.startswith('http'):
Expand Down
2 changes: 1 addition & 1 deletion lumen/sources/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def create_sql_expr_source(
return source

def get_tables(self):
if isinstance(self.tables, (dict, list)):
if isinstance(self.tables, dict | list):
return list(self.tables)
return [t[0] for t in self._connection.execute('SHOW TABLES').fetchall()]

Expand Down
7 changes: 4 additions & 3 deletions lumen/transforms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import datetime as dt
import hashlib

from collections.abc import Callable
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar, Literal, Union,
TYPE_CHECKING, Any, ClassVar, Literal,
)

import numpy as np
Expand All @@ -27,8 +28,8 @@
if TYPE_CHECKING:
from dask.dataframe import DataFrame as dDataFrame, Series as dSeries
from panel.viewable import Viewable
DataFrame = Union[pd.DataFrame, dDataFrame]
Series = Union[pd.Series, dSeries]
DataFrame = pd.DataFrame | dDataFrame
Series = pd.Series | dSeries


class Transform(MultiTypeComponent):
Expand Down
2 changes: 1 addition & 1 deletion lumen/ui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _update_spec(self, *events):
def select(self, selector=None):
items = super().select(selector)
for values in self.param.objects().values():
if isinstance(values, (list, dict)):
if isinstance(values, list | dict):
values = values.values() if isinstance(values, dict) else values
items += [o for v in values if isinstance(v, Viewable) for o in v.select(selector)]
elif isinstance(values, Viewable):
Expand Down
2 changes: 1 addition & 1 deletion lumen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def expand_spec(pars, context={}, getenv=True, getshell=True, getheaders=True,
return {k: expand_spec(
v, context, getenv, getshell, getheaders, getcookies, getoauth
) for k, v in pars.items()}
elif isinstance(pars, (list, tuple, set)):
elif isinstance(pars, list | tuple | set):
return type(pars)(expand_spec(
v, context, getenv, getshell, getheaders, getcookies, getoauth
) for v in pars)
Expand Down
4 changes: 2 additions & 2 deletions lumen/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import re
import textwrap

from collections.abc import Sequence
from collections.abc import Callable, Sequence
from difflib import get_close_matches
from inspect import signature
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any

import yaml

Expand Down
Loading
Loading