Files
cadquery/mypy/cadquery-plugin.py
AU 995d1393bd Free function api (#1469)
* Initial free functions

* Mypy fixes

* Add a mypy plugin that handles _get*

* More helpers and fixes

* black

* More hooks

* More ops and primitives

* Fill with constraints and cap

* Minimal docstrings and mypy fix

* Bool op operators for Shape

* Extra docstring

* Added spline primitive

* Added alternative constructors

* Update solid

* Add shape normalization

* Add text

* Added moved overload

* Another moved overload

* Convert location constructor to multimethod

* Additional Loc constructor

* Extra vertex constructor

* Additional cone overload

* Start with tests

* Fix compouund normalization

* Bool op tests

* Additional Location overload

* test moved and fix bool ops

* Different cap params

* More tests

* Test revolve and offset

* Test sweep and loft

* Add bool ops

* More tests

* Test text

* Improve coverage for utils

* More move[d] and Location overloads

* Start working on some docs

* Update index

* Doc fix

* Typo fix

* More move/moved overloads

* Small doc update

* Better Location coverage

* Fix angle units in Location

* More docs and a usability fix

* Cosmetics

* Mypy fix

* Remove dead code

* Coverage tweaks

* More docs'

* Box centering and box/plane arg order

* Docs cosmetics - nicer sweep

* Apply suggestions

Co-authored-by: Jeremy Wright <wrightjmf@gmail.com>

* Add docstrings

* Doc tweaks

* Bump multimethod version

* Add occ_impl.shapes

* Mention free funcs in the primer

* Typos

* Typo

* Punctuation

---------

Co-authored-by: Jeremy Wright <wrightjmf@gmail.com>
2024-05-05 11:25:45 +02:00

82 lines
2.0 KiB
Python

from mypy.plugin import Plugin, FunctionContext
from mypy.types import Type, UnionType
class CadqueryPlugin(Plugin):
def get_function_hook(self, fullname: str):
if fullname == "cadquery.occ_impl.shapes._get":
return hook__get
elif fullname == "cadquery.occ_impl.shapes._get_one":
return hook__get_one
elif fullname == "cadquery.occ_impl.shapes._get_edges":
return hook__get_edges
elif fullname == "cadquery.occ_impl.shapes._get_wires":
return hook__get_wires
return None
def hook__get(ctx: FunctionContext) -> Type:
"""
Hook for cq.occ_impl.shapes._get
Based on the second argument values it adjusts return type to an Iterator of specific subclasses of Shape.
"""
if hasattr(ctx.args[1][0], "items"):
return_type_names = [el.value for el in ctx.args[1][0].items]
else:
return_type_names = [ctx.args[1][0].value]
return_types = UnionType([ctx.api.named_type(n) for n in return_type_names])
return ctx.api.named_generic_type("typing.Iterable", [return_types])
def hook__get_one(ctx: FunctionContext) -> Type:
"""
Hook for cq.occ_impl.shapes._get_one
Based on the second argument values it adjusts return type to a Union of specific subclasses of Shape.
"""
if hasattr(ctx.args[1][0], "items"):
return_type_names = [el.value for el in ctx.args[1][0].items]
else:
return_type_names = [ctx.args[1][0].value]
return UnionType([ctx.api.named_type(n) for n in return_type_names])
def hook__get_wires(ctx: FunctionContext) -> Type:
"""
Hook for cq.occ_impl.shapes._get_wires
"""
return_type = ctx.api.named_type("Wire")
return ctx.api.named_generic_type("typing.Iterable", [return_type])
def hook__get_edges(ctx: FunctionContext) -> Type:
"""
Hook for cq.occ_impl.shapes._get_edges
"""
return_type = ctx.api.named_type("Edge")
return ctx.api.named_generic_type("typing.Iterable", [return_type])
def plugin(version: str):
return CadqueryPlugin