Skip to content

Code style, Python type hinting #287

@samlyme

Description

@samlyme

There are many examples of poorly written python code, leading to type hinting errors, even if the code runs.

For example, on line 279 of gym_pybullet_drones/control/DSLPIDControl.py, we see:

        DIM = len(np.array(thrust))
        pwm = np.clip((np.sqrt(np.array(thrust)/(self.KF*(4/DIM)))-self.PWM2RPM_CONST)/self.PWM2RPM_SCALE, self.MIN_PWM, self.MAX_PWM)
        if DIM in [1, 4]:
            return np.repeat(pwm, 4/DIM)
        elif DIM==2:
            return np.hstack([pwm, np.flip(pwm)])
        else:
            print("[ERROR] in DSLPIDControl._one23DInterface()")
            exit()

Leading to the type error:

No overloads for "repeat" match the provided arguments
Argument of type "float" cannot be assigned to parameter "repeats" of type "_ArrayLikeInt_co" in function "repeat"
  Type "float" is not assignable to type "_ArrayLikeInt_co"
    "float" is incompatible with protocol "_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any]]]"
      "__array__" is not present
    "float" is incompatible with protocol "_NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any]]]]"
      "__len__" is not present
      "__getitem__" is not present
      "__contains__" is not present
      "__iter__" is not present
  ...

This is because np.repeat expects an integer value for the Dimensions. Thus,

        DIM = len(np.array(thrust))
        pwm = np.clip((np.sqrt(np.array(thrust)/(self.KF*(4/DIM)))-self.PWM2RPM_CONST)/self.PWM2RPM_SCALE, self.MIN_PWM, self.MAX_PWM)
        if DIM in [1, 4]:
            return np.repeat(pwm, 4//DIM)
        elif DIM==2:
            return np.hstack([pwm, np.flip(pwm)])
        else:
            print("[ERROR] in DSLPIDControl._one23DInterface()")
            exit()

Fixes the type issue.

I love this project and would love to contribute! I am working on a just generally cleaning the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions