Skip to content

Commit 89577d9

Browse files
committed
More type-related fixes
1 parent 0bb5619 commit 89577d9

6 files changed

Lines changed: 37 additions & 27 deletions

File tree

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,30 @@ Development environment notes:
138138
to attach the vscode debugger to the running process as well as reload after you've made changes.
139139
- I've tried to leave the boilerplate files generated by Fusion's `Create script or add-in` relatively untouched, so
140140
that the code can be as modular as possible. Unused boilerplate has been removed where possible.
141-
- There are `# type: ignore` comments throughout the code to get
142-
[Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) to stop complaining. I would
143-
rather not need them, but I'm not sure how to improve this. Any advice or PRs that improve type checking would be
144-
greatly appreciated.
141+
- There are many seemingly extraneous `cast()` calls throughout the code to help
142+
[Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) understand what's going on, because [many classes in the underlying adsk libraries aren't proper Enums](https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/faulty-enum-types/m-p/12066483).
145143
- I've been using a `.vscode/settings.json` file that looks like this, but it's gitignored because it constains local
146144
paths. AFAIK, there's no way to write these paths in a user or operating system agnostic way. Until there's a better
147145
solution, I recommend creating a throwaway Fusion add-in from the `Scripts and Add-Ins` dialog just to grab those
148146
paths, then create your own local `.vscode/settings.json` file in this project using the paths it generates. You'll
149-
probably also want to add in the `files.exclude` section to hide auto-generated temp files.
147+
probably also want to add in the other stuff from here, to be consistent.
150148

151149
```json
152150
{
153151
"python.autoComplete.extraPaths": ["C:/Users/Cowboy/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Python/defs"],
154152
"python.analysis.extraPaths": ["C:/Users/Cowboy/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Python/defs"],
155-
"python.defaultInterpreterPath": "C:/Users/Cowboy/AppData/Local/Autodesk/webdeploy/production/2df205e6add6cffdc9b4f8421c671a7d78103826/Python/python.exe",
153+
"python.defaultInterpreterPath": "C:/Users/Cowboy/AppData/Local/Autodesk/webdeploy/production/7627f627889be835182cfc345110c3c9f5bc9cc3/Python/python.exe",
156154
"files.exclude": {
157155
"**/__pycache__/": true,
158156
"**/*.py[codz]": true,
159157
"**/*$py.class": true
160-
}
161-
}
158+
},
159+
"editor.tabSize": 4,
160+
"editor.defaultFormatter": "charliermarsh.ruff",
161+
"ruff.lineLength": 160,
162+
"python.analysis.typeCheckingMode": "standard",
163+
"python.analysis.diagnosticsSource": "Pylance"
164+
}
162165
```
163166

164167
Files of interest:

lib/generalUtils/debug_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from enum import Enum
21
import adsk.core
32
import adsk.fusion
43
from ..fusionAddInUtils import log

lib/generalUtils/extrude_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import adsk.core
22
import adsk.fusion
33
from .value_utils import getNormalizedValueInput
4-
from typing import TypedDict, NotRequired, Unpack
4+
from typing import TypedDict, NotRequired, Unpack, cast
55

66
# Extrude Feature API Sample
77
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-CB1A2357-C8CD-474D-921E-992CA3621D04
@@ -22,7 +22,7 @@ def extrude(
2222
name: str,
2323
**kwargs: Unpack[ExtrudeKwargs],
2424
):
25-
operation = kwargs.get("operation", adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
25+
operation = kwargs.get("operation", cast(adsk.fusion.FeatureOperations, adsk.fusion.FeatureOperations.NewBodyFeatureOperation))
2626
offsetFrom = kwargs.get("offsetFrom")
2727

2828
profiles = adsk.core.ObjectCollection.create()
@@ -32,9 +32,9 @@ def extrude(
3232
features = component.features
3333
extrudeFeatures = features.extrudeFeatures
3434

35-
extrudeInput = extrudeFeatures.createInput(profiles, operation) # type: ignore
35+
extrudeInput = extrudeFeatures.createInput(profiles, operation)
3636
extent = adsk.fusion.DistanceExtentDefinition.create(getNormalizedValueInput(height))
37-
extrudeInput.setOneSideExtent(extent, adsk.fusion.ExtentDirections.PositiveExtentDirection) # type: ignore
37+
extrudeInput.setOneSideExtent(extent, cast(adsk.fusion.ExtentDirections, adsk.fusion.ExtentDirections.PositiveExtentDirection))
3838
if offsetFrom:
3939
extrudeInput.startExtent = adsk.fusion.FromEntityStartDefinition.create(offsetFrom, adsk.core.ValueInput.createByReal(0))
4040

lib/generalUtils/sketch_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import adsk.core
22
import adsk.fusion
33
import math
4-
from typing import TypedDict, NotRequired, Unpack
4+
from typing import TypedDict, NotRequired, Unpack, cast
55

66
# Sketch Object
77
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-2367ed6a-0ad1-4c8f-935e-b52738d1ce2b
@@ -137,13 +137,13 @@ def constrainRectangleWidthHeight(
137137
dimensions.addDistanceDimension(
138138
lines.item(0).startSketchPoint,
139139
lines.item(0).endSketchPoint,
140-
adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation, # type: ignore
140+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation),
141141
lineMidpoint(lines.item(0), -labelOffset),
142142
)
143143
dimensions.addDistanceDimension(
144144
lines.item(1).startSketchPoint,
145145
lines.item(1).endSketchPoint,
146-
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation, # type: ignore
146+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.VerticalDimensionOrientation),
147147
lineMidpoint(lines.item(1), labelOffset),
148148
)
149149

@@ -162,12 +162,12 @@ def constrainPointToPoint(
162162
dimensions.addDistanceDimension(
163163
referencePoint,
164164
sketchPoint,
165-
adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation, # type: ignore
165+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation),
166166
midpoint(point(referencePoint.geometry.x, sketchPoint.geometry.y), sketchPoint.geometry),
167167
)
168168
dimensions.addDistanceDimension(
169169
referencePoint,
170170
sketchPoint,
171-
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation, # type: ignore
171+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.VerticalDimensionOrientation),
172172
midpoint(point(sketchPoint.geometry.x, referencePoint.geometry.y), sketchPoint.geometry),
173173
)

lib/panelUtils/panel_generate.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import adsk.core
22
import adsk.fusion
3+
from typing import cast
34
from ..generalUtils.sketch_utils import (
45
addPoints,
56
constrainPointToPoint,
@@ -114,7 +115,7 @@ def addRefLine(panelLine: adsk.fusion.SketchLine, offset: float):
114115
dimensions.addDistanceDimension(
115116
panelLine.startSketchPoint,
116117
line.startSketchPoint,
117-
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation, # type: ignore
118+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.VerticalDimensionOrientation),
118119
midpoint(lineMidpoint(line), lineMidpoint(panelLine)),
119120
)
120121
return line
@@ -126,7 +127,7 @@ def addRefLine(panelLine: adsk.fusion.SketchLine, offset: float):
126127
dimensions.addDistanceDimension(
127128
topRefLine.startSketchPoint,
128129
bottomRefLine.startSketchPoint,
129-
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation, # type: ignore
130+
cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.VerticalDimensionOrientation),
130131
addPoints(
131132
midpoint(topRefLine.startSketchPoint.geometry, bottomRefLine.startSketchPoint.geometry),
132133
point(-0.2, 0),
@@ -190,8 +191,8 @@ def addRefLine(panelLine: adsk.fusion.SketchLine, offset: float):
190191
-opts.supportSolidHeight,
191192
"Support",
192193
offsetFrom=body1.faces.item(5 + slotFaceCount),
193-
operation=adsk.fusion.FeatureOperations.JoinFeatureOperation,
194-
) # type: ignore
194+
operation=cast(adsk.fusion.FeatureOperations, adsk.fusion.FeatureOperations.JoinFeatureOperation),
195+
)
195196
elif opts.supportType == "shell":
196197
body1 = extrude(component, sketch, [0, 1, 2, 3], -opts.panelHeight, "Panel")
197198
body = extrude(
@@ -201,8 +202,8 @@ def addRefLine(panelLine: adsk.fusion.SketchLine, offset: float):
201202
-opts.supportShellHeight,
202203
"Support Shell",
203204
offsetFrom=body1.faces.item(5 + slotFaceCount),
204-
operation=adsk.fusion.FeatureOperations.JoinFeatureOperation,
205-
) # type: ignore
205+
operation=cast(adsk.fusion.FeatureOperations, adsk.fusion.FeatureOperations.JoinFeatureOperation),
206+
)
206207
else:
207208
body = extrude(component, sketch, [0], -opts.panelHeight, "Panel")
208209

lib/panelUtils/panel_inputs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import adsk.core
22
from enum import Enum
3+
from typing import cast
34

45
app = adsk.core.Application.get()
56
ui = app.userInterface
@@ -83,7 +84,9 @@ def initializeInputs(self):
8384
message = 'For more information, <a href="https://github.com/cowboy/ModularSynthPanelGenerator">read the documentation.</a>'
8485
self.inputs.addTextBoxCommandInput("infoTextBox", "Information", message, 1, True)
8586

86-
heightDropdown = self.inputs.addDropDownCommandInput("formatType", "Panel format", adsk.core.DropDownStyles.TextListDropDownStyle) # type: ignore
87+
heightDropdown = self.inputs.addDropDownCommandInput(
88+
"formatType", "Panel format", cast(adsk.core.DropDownStyles, adsk.core.DropDownStyles.TextListDropDownStyle)
89+
)
8790
for name in self.options.formatNames:
8891
heightDropdown.listItems.add(name, name == self.options.formatName)
8992

@@ -95,14 +98,18 @@ def initializeInputs(self):
9598
adsk.core.ValueInput.createByReal(self.options.panelHeight),
9699
)
97100

98-
anchorPointDropdown = self.inputs.addDropDownCommandInput("anchorPoint", "Anchor point", adsk.core.DropDownStyles.TextListDropDownStyle) # type: ignore
101+
anchorPointDropdown = self.inputs.addDropDownCommandInput(
102+
"anchorPoint", "Anchor point", cast(adsk.core.DropDownStyles, adsk.core.DropDownStyles.TextListDropDownStyle)
103+
)
99104
for name in self.options.anchorPointNames:
100105
anchorPointDropdown.listItems.add(name, name == self.options.anchorPointName)
101106

102107
supportGroup = self.inputs.addGroupCommandInput("supportGroup", "Reinforcement")
103108
supportGroup.isExpanded = True
104109

105-
supportTypeDropdown = supportGroup.children.addDropDownCommandInput("supportType", "Type", adsk.core.DropDownStyles.TextListDropDownStyle) # type: ignore
110+
supportTypeDropdown = supportGroup.children.addDropDownCommandInput(
111+
"supportType", "Type", cast(adsk.core.DropDownStyles, adsk.core.DropDownStyles.TextListDropDownStyle)
112+
)
106113
for name in self.options.supportTypeNames:
107114
supportTypeDropdown.listItems.add(name, name == self.options.supportTypeName)
108115

0 commit comments

Comments
 (0)