r/learnpython•
drBonkers

Type Hints and Numpy Functions Returning `Any`

Hello, I have a function that uses the np.argmax function to return the index that holds the maximal value in a 1D slice of a 2D ndarray.

def query(self,  # Update the state without updating the Q-table.
                  s: int,  # The state to update to.
                  ) -> int:  # The maximally-rewarded action to take from this state.
    self.state = s

    maximally_rewarded_action: int = np.argmax(self.q_table[self.state])  # `argmax`'s documentation states it returns `Any` and my static analysis thinks it returns an `ndarray`.
    return maximally_rewarded_action

How should I write this to satisfy the static analyzer?

Posted Nov 2, 2023, 1:23 AM UTC

312
Loading comments…