2

I have multiple numpy arrays with different objects. How can I give 'type-hint' to the Pycharm so that I can get methods of that class while coding? For example,

import numpy as np

class Test:
    def do_task():
       pass

data = [Test(), Test(), Test()]
my_array = np.array(data, dtype=Test)

def test_hint(array:np.ndarray):  # <-- I can give typehint as array but I also want to give hint about what kind of array it is.
    for a in array:
         a.... # Here I want Pycharm to give me list of methods from 'Test' class

I can always explicitly mention what kind of object it is like following,

for a in array:
   temp = a # type: Test
   temp... # Here Pycharm will give me all the suggestions.

Is there any way I can provide type-hint in the constructor so that I do not need to write additional line of code to declare type of data? I tried looking at python's type module for some clue, but couldn't find any.

3
  • Why are you using array here? Iterating on data would be faster. It might not help with your hint problem, but I expect there's more discussion about doing this with lists than with arrays. Commented Nov 15, 2019 at 17:25
  • @hpaulj I have to use array because my program requires lot of reshaping of the data which is much easier with numpy array or matrix than regular iterators. Commented Nov 15, 2019 at 18:01
  • Look here: stackoverflow.com/questions/59179493/… Commented Jan 14, 2020 at 14:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.