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

Add __all__ to more files #594

Merged
merged 8 commits into from
Jan 30, 2025
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
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ target-version = "py311"

[tool.ruff.lint]
select = [
"F", # pyflakes
"E", # pycodestyle
"W", # pycodestyle
"UP", # pyupgrade
"N", # pep8-naming
"F", # pyflakes
"E", # pycodestyle
"W", # pycodestyle
"UP", # pyupgrade
"N", # pep8-naming
# "D", # pydocstyle
"I", # isort
"ANN", # annotations
"RUF", # ruff
"B",
"B", # bugbear
]
ignore = ["ANN401"]
ignore = ["ANN401", "D100"]
per-file-ignores = { "tests/*.py" = ["D"] }

[tool.ruff.format]
Expand Down
5 changes: 3 additions & 2 deletions src/kfactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import klayout.lay as lay
import klayout.rdb as rdb


from .conf import config, logger
from .conf import config, logger, CheckInstances
from .enclosure import KCellEnclosure, LayerEnclosure
from .grid import flexgrid, flexgrid_dbu, grid, grid_dbu
from .kcell import BaseKCell, DKCell, KCell, VKCell, show
Expand Down Expand Up @@ -47,8 +46,10 @@
typings,
)


__all__ = [
"BaseKCell",
"CheckInstances",
"Constants",
"DInstance",
"DInstanceGroup",
Expand Down
Empty file removed src/kfactory/_cells.py
Empty file.
9 changes: 4 additions & 5 deletions src/kfactory/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
from . import kdb, rdb

if TYPE_CHECKING:
from .kcell import ProtoKCell
from .kcell import AnyKCell
from .layout import KCLayout

__all__ = ["LogLevel", "config"]


DEFAULT_TRANS: dict[str, str | int | float | dict[str, str | int | float]] = {
"x": "E",
Expand Down Expand Up @@ -54,7 +56,7 @@ class PROPID(IntEnum):
class ShowFunction(Protocol):
def __call__(
self,
layout: KCLayout | ProtoKCell[Any] | Path | str,
layout: KCLayout | AnyKCell | Path | str,
*,
lyrdb: rdb.ReportDatabase | Path | str | None,
l2n: kdb.LayoutToNetlist | Path | str | None,
Expand Down Expand Up @@ -300,6 +302,3 @@ def __init__(self, **data: Any) -> None:


config = Settings()


__all__ = ["LogLevel", "config"]
18 changes: 16 additions & 2 deletions src/kfactory/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

if TYPE_CHECKING:
from .instance import ProtoInstance
from .kcell import ProtoKCell
from .kcell import AnyKCell
from .layout import KCLayout
from .port import ProtoPort

__all__ = [
"CellNameError",
"InvalidLayerError",
"LockedError",
"MergeError",
"PortLayerMismatchError",
"PortTypeMismatchError",
"PortWidthMismatchError",
]


class LockedError(AttributeError):
"""Raised when a locked cell is being modified."""

def __init__(self, kcell: ProtoKCell[Any]) -> None:
def __init__(self, kcell: AnyKCell) -> None:
"""Throw _locked error."""
super().__init__(
f"{kcell.name!r} is locked and likely stored in cache. Modifications are "
Expand Down Expand Up @@ -126,3 +136,7 @@ def __init__(

class CellNameError(ValueError):
"""Raised if a KCell is created and the automatic assigned name is taken."""


class InvalidLayerError(ValueError):
"""Raised when a layer is not valid."""
14 changes: 11 additions & 3 deletions src/kfactory/geometry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Generic, Self, overload
from typing import TYPE_CHECKING, Any, Generic, Self, overload

import numpy as np

Expand All @@ -13,6 +13,8 @@
from .layer import LayerEnum
from .layout import KCLayout

__all__ = ["DBUGeometricObject", "GeometricObject", "SizeInfo", "UMGeometricObject"]


class SizeInfo(Generic[TUnit]):
_bf: BoxFunction[TUnit]
Expand Down Expand Up @@ -110,7 +112,13 @@ def center(self) -> tuple[TUnit, TUnit]:


class GeometricObject(Generic[TUnit], ABC):
kcl: KCLayout
@property
@abstractmethod
def kcl(self) -> KCLayout: ...

@kcl.setter
@abstractmethod
def kcl(self, val: KCLayout, /) -> None: ...

@abstractmethod
def bbox(self, layer: int | None = None) -> BoxLike[TUnit]: ...
Expand All @@ -135,7 +143,7 @@ def transform(
self,
trans: kdb.Trans | kdb.DTrans | kdb.ICplxTrans | kdb.DCplxTrans,
/,
) -> None: ...
) -> Any: ...

@property
def x(self) -> TUnit:
Expand Down
2 changes: 2 additions & 0 deletions src/kfactory/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from .instance_group import DInstanceGroup, InstanceGroup
from .kcell import DKCell, KCell

__all__ = ["flexgrid", "flexgrid_dbu", "grid", "grid_dbu"]


def grid_dbu(
target: KCell,
Expand Down
61 changes: 28 additions & 33 deletions src/kfactory/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@
PortWidthMismatchError,
)
from .geometry import DBUGeometricObject, GeometricObject, UMGeometricObject
from .instance_ports import (
DInstancePorts,
InstancePorts,
ProtoInstancePorts,
ProtoTInstancePorts,
VInstancePorts,
)
from .layer import LayerEnum
from .port import DPort, Port, ProtoPort
from .serialization import clean_name, get_cell_name
from .settings import Info, KCellSettings
from .typings import TUnit

if TYPE_CHECKING:
from .kcell import DKCell, KCell, ProtoTKCell, VKCell
from .instance_ports import (
DInstancePorts,
InstancePorts,
ProtoInstancePorts,
ProtoTInstancePorts,
VInstancePorts,
)
from .kcell import AnyKCell, AnyTKCell, DKCell, KCell, ProtoTKCell, VKCell
from .layout import KCLayout

__all__ = ["DInstance", "Instance", "ProtoInstance", "ProtoTInstance", "VInstance"]


class ProtoInstance(GeometricObject[TUnit], Generic[TUnit]):
"""Base class for instances."""
Expand All @@ -50,8 +52,8 @@
return self._kcl

@kcl.setter
def kcl(self, value: KCLayout) -> None:
self._kcl = value
def kcl(self, val: KCLayout) -> None:
self._kcl = val

@property
@abstractmethod
Expand All @@ -72,7 +74,7 @@

@property
@abstractmethod
def ports(self) -> ProtoInstancePorts[TUnit]: ...
def ports(self) -> ProtoInstancePorts[TUnit, ProtoInstance[TUnit]]: ...


class ProtoTInstance(ProtoInstance[TUnit], Generic[TUnit]):
Expand Down Expand Up @@ -402,6 +404,8 @@
op.dcplx_trans.disp - p.dcplx_trans.disp
)
self.dmirror_y(op.dcplx_trans.disp.y)
case _:
...

Check warning on line 408 in src/kfactory/instance.py

View check run for this annotation

Codecov / codecov/patch

src/kfactory/instance.py#L407-L408

Added lines #L407 - L408 were not covered by tests

else:
conn_trans = kdb.Trans.M90 if mirror else kdb.Trans.R180
Expand All @@ -422,6 +426,8 @@
case True, False:
self._instance.trans = kdb.Trans(op.trans.disp - p.trans.disp)
self.dmirror_y(op.dcplx_trans.disp.y)
case _:
...

Check warning on line 430 in src/kfactory/instance.py

View check run for this annotation

Codecov / codecov/patch

src/kfactory/instance.py#L429-L430

Added lines #L429 - L430 were not covered by tests

def __repr__(self) -> str:
"""Return a string representation of the instance."""
Expand Down Expand Up @@ -463,23 +469,18 @@
"""

yaml_tag: ClassVar[str] = "!Instance"
_ports: InstancePorts

def __init__(self, kcl: KCLayout, instance: kdb.Instance) -> None:
"""Create an instance from a KLayout Instance."""
self.kcl = kcl
self._instance = instance
self._ports = InstancePorts(self)

@property
def ports(self) -> InstancePorts:
"""Gets the transformed ports of the KCell."""
return self._ports
from .instance_ports import InstancePorts

@ports.setter
def ports(self, value: InstancePorts) -> None:
"""Sets the transformed ports of the KCell."""
self._ports = value
return InstancePorts(self)

def __getitem__(
self, key: int | str | None | tuple[int | str | None, int, int]
Expand All @@ -498,7 +499,6 @@
3 times in `a` direction (4th index in the array), and 5 times in `b` direction
(5th index in the array).
"""

return Port(base=self.ports[key].base)

@property
Expand Down Expand Up @@ -547,23 +547,18 @@
"""

yaml_tag: ClassVar[str] = "!Instance"
_ports: DInstancePorts

def __init__(self, kcl: KCLayout, instance: kdb.Instance) -> None:
"""Create an instance from a KLayout Instance."""
self.kcl = kcl
self._instance = instance
self._ports = DInstancePorts(self)

@property
def ports(self) -> DInstancePorts:
"""Gets the transformed ports of the KCell."""
return self._ports
from .instance_ports import DInstancePorts

@ports.setter
def ports(self, value: DInstancePorts) -> None:
"""Sets the transformed ports of the KCell."""
self._ports = value
return DInstancePorts(self)

@property
def cell(self) -> DKCell:
Expand Down Expand Up @@ -616,7 +611,6 @@
_name: str | None
cell: VKCell | KCell
trans: kdb.DCplxTrans
_ports: VInstancePorts

def __init__(
self,
Expand All @@ -628,7 +622,6 @@
self._name = name
self.cell = cell
self.trans = trans or kdb.DCplxTrans()
self._ports = VInstancePorts(self)

@property
def name(self) -> str | None:
Expand Down Expand Up @@ -667,7 +660,9 @@

@property
def ports(self) -> VInstancePorts:
return self._ports
from .instance_ports import VInstancePorts

return VInstancePorts(self)

def __repr__(self) -> str:
"""Return a string representation of the instance."""
Expand All @@ -676,7 +671,7 @@

def insert_into(
self,
cell: ProtoTKCell[Any],
cell: AnyTKCell,
trans: kdb.DCplxTrans | None = None,
) -> Instance:
from .kcell import KCell, VKCell
Expand Down Expand Up @@ -758,7 +753,7 @@
@overload
def insert_into_flat(
self,
cell: ProtoTKCell[Any] | VKCell,
cell: AnyKCell,
trans: kdb.DCplxTrans | None = None,
*,
levels: None = None,
Expand All @@ -767,15 +762,15 @@
@overload
def insert_into_flat(
self,
cell: ProtoTKCell[Any] | VKCell,
cell: AnyKCell,
*,
trans: kdb.DCplxTrans | None = None,
levels: int,
) -> None: ...

def insert_into_flat(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): We've found these issues:


Explanation

The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.

How can you solve this?

It might be worth refactoring this function to make it shorter and more readable.

  • Reduce the function length by extracting pieces of functionality out into
    their own functions. This is the most important thing you can do - ideally a
    function should be less than 10 lines.
  • Reduce nesting, perhaps by introducing guard clauses to return early.
  • Ensure that variables are tightly scoped, so that code using related concepts
    sits together within the function rather than being scattered.

self,
cell: ProtoTKCell[Any] | VKCell,
cell: AnyKCell,
trans: kdb.DCplxTrans | None = None,
*,
levels: int | None = None,
Expand Down
8 changes: 8 additions & 0 deletions src/kfactory/instance_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
if TYPE_CHECKING:
from .layout import KCLayout

__all__ = [
"DInstanceGroup",
"InstanceGroup",
"ProtoInstanceGroup",
"ProtoTInstanceGroup",
"VInstanceGroup",
]


class ProtoInstanceGroup(Generic[TUnit, TInstance], GeometricObject[TUnit]):
insts: list[TInstance]
Expand Down
Loading
Loading