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 1 commit
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
2 changes: 1 addition & 1 deletion src/kfactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import klayout.lay as lay
import klayout.rdb as rdb


from .conf import config, logger
from .enclosure import KCellEnclosure, LayerEnclosure
from .grid import flexgrid, flexgrid_dbu, grid, grid_dbu
Expand Down Expand Up @@ -47,6 +46,7 @@
typings,
)


__all__ = [
"BaseKCell",
"Constants",
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",
"PortLayerMismatch",
"PortTypeMismatch",
"PortWidthMismatch",
]


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
22 changes: 14 additions & 8 deletions src/kfactory/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
from .typings import TUnit

if TYPE_CHECKING:
from .kcell import DKCell, KCell, ProtoTKCell, VKCell
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 @@ def kcl(self) -> KCLayout:
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 @@ def __getitem__(self, key: int | str | None) -> ProtoPort[TUnit]: ...

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


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

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

def __repr__(self) -> str:
"""Return a string representation of the instance."""
Expand Down Expand Up @@ -675,7 +681,7 @@ def __repr__(self) -> str:

def insert_into(
self,
cell: ProtoTKCell[Any],
cell: AnyTKCell,
trans: kdb.DCplxTrans | None = None,
) -> Instance:
from .kcell import KCell, VKCell
Expand Down Expand Up @@ -757,7 +763,7 @@ def insert_into(
@overload
def insert_into_flat(
self,
cell: ProtoTKCell[Any] | VKCell,
cell: AnyKCell,
trans: kdb.DCplxTrans | None = None,
*,
levels: None = None,
Expand All @@ -766,15 +772,15 @@ def insert_into_flat(
@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
29 changes: 18 additions & 11 deletions src/kfactory/instance_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,34 @@
filter_regex,
)
from .ports import DPorts, Ports, ProtoPorts
from .typings import TUnit
from .typings import TInstance, TUnit
from .utilities import pprint_ports

if TYPE_CHECKING:
from .instance import DInstance, Instance, ProtoTInstance, VInstance

__all__ = [
"DInstancePorts",
"InstancePorts",
"ProtoInstancePorts",
"ProtoTInstancePorts",
"VInstancePorts",
]


class HasCellPorts(Generic[TUnit], ABC):
@property
@abstractmethod
def cell_ports(self) -> ProtoPorts[TUnit]: ...


class ProtoInstancePorts(HasCellPorts[TUnit], ABC): ...
class ProtoInstancePorts(HasCellPorts[TUnit], Generic[TUnit, TInstance], ABC):
instance: TInstance


class ProtoTInstancePorts(ProtoInstancePorts[TUnit], ABC):
class ProtoTInstancePorts(
ProtoInstancePorts[TUnit, ProtoTInstance[TUnit]], Generic[TUnit], ABC
):
"""Ports of an Instance.

These act as virtual ports as the centers needs to change if the
Expand All @@ -53,7 +64,7 @@ class ProtoTInstancePorts(ProtoInstancePorts[TUnit], ABC):

def __len__(self) -> int:
"""Return Port count."""
if not self.instance.is_regular_array():
if not self.instance.instance.is_regular_array():
return len(self.cell_ports)
else:
return len(self.cell_ports) * self.instance.na * self.instance.nb
Expand Down Expand Up @@ -300,8 +311,6 @@ def copy(


class InstancePorts(ProtoTInstancePorts[int]):
instance: Instance

def __init__(self, instance: Instance) -> None:
"""Creates the virtual ports object.

Expand All @@ -312,7 +321,7 @@ def __init__(self, instance: Instance) -> None:

@property
def cell_ports(self) -> Ports:
return self.instance.cell.ports
return Ports(kcl=self.instance.cell.kcl, bases=self.instance.cell.ports.bases)

def filter(
self,
Expand All @@ -334,8 +343,6 @@ def __getitem__(


class DInstancePorts(ProtoTInstancePorts[float]):
instance: DInstance

def __init__(self, instance: DInstance) -> None:
"""Creates the virtual ports object.

Expand All @@ -346,7 +353,7 @@ def __init__(self, instance: DInstance) -> None:

@property
def cell_ports(self) -> DPorts:
return self.instance.cell.ports
return DPorts(kcl=self.instance.cell.kcl, bases=self.instance.cell.ports.bases)

def filter(
self,
Expand All @@ -367,7 +374,7 @@ def __getitem__(
return DPort(base=super().__getitem__(key).base)


class VInstancePorts(ProtoInstancePorts[float]):
class VInstancePorts(ProtoInstancePorts[float, VInstance]):
"""Ports of an instance.

These act as virtual ports as the centers needs to change if the
Expand Down
8 changes: 8 additions & 0 deletions src/kfactory/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
if TYPE_CHECKING:
from .kcell import TKCell

__all__ = [
"DInstances",
"Instances",
"ProtoInstances",
"ProtoTInstances",
"VInstances",
]


class ProtoInstances(Generic[TUnit, TInstance], ABC):
@abstractmethod
Expand Down
Loading
Loading