Faulty Enum Types

Faulty Enum Types

felix85SA3
Participant Participant
442 Views
3 Replies
Message 1 of 4

Faulty Enum Types

felix85SA3
Participant
Participant

I'm new to Python and used to strict type checking, so I just set up my VSCode and found that the enumeration types that are used throughout the definition files (%appdata%\Autodesk\Autodesk Fusion 360\API\Python\defs\fusion.py) lack Enum-inheritance

 

E.g. createInput is defined as:

def createInput(self, profile: core.Base, operation: FeatureOperations) -> ExtrudeFeatureInput:

But the class FeatureOperations reads:

class FeatureOperations():
    """
    List of the different operations a feature can perform.
    """
    def __init__(self):
        pass
    JoinFeatureOperation = 0
    CutFeatureOperation = 1
    IntersectFeatureOperation = 2
    NewBodyFeatureOperation = 3
    NewComponentFeatureOperation = 4

Which yields an error

Argument of type "int" cannot be assigned to parameter "operation" of type "FeatureOperations" in function "createInput"
  "int" is incompatible with "FeatureOperations"

 

According to my understanding the following would fix the issue:

from enum import Enum

class FeatureOperations(Enum):
    # ...

 

 Am I missing something?

443 Views
3 Replies
Replies (3)
Message 2 of 4

benalman
Observer
Observer

I've run into this exact same issue. Did you figure out a workaround, other than adding "# type: ignore" everywhere?

 

Fusion Python API maintainers - please fix this!

0 Likes
Message 3 of 4

BrianEkins
Mentor
Mentor

I think the problem you're referring to is a result of the Fusion API not using the Python Enum class, but instead defines enums using a standard class with variables with integer values that define the enum values. The reason for that is that Python Enums didn't exist in the language when the API was first released, and this was the workaround chosen. It would be beneficial if the API could be updated to utilize the new Python Enum class; however, I am unsure if that is possible without breaking all existing code that uses enums. I don't know if the possibility has been investigated or not.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 4

benalman
Observer
Observer

In the meantime, I have started calling cast() explicitly whenever I use an adsk Enum-like value. It adds more code, but it allows type hinting to work.

 

Before:

dimensions.addDistanceDimension(
  startPoint,
  endPoint,
  adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation, # type: ignore
  textPoint
)

 

After:

from typing import cast

dimensions.addDistanceDimension(
  startPoint,
  endPoint,
  cast(adsk.fusion.DimensionOrientations, adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation),
  textPoint
)

 

I'm not sure if it's possible to create macros in Python, but if it is, that could possible reduce some of the extra code.

0 Likes