How to hide PyCharm autocomplete for specific class

Is there a way to turn off autocomplete in Pycharm for a specific class?

I have a class Result that may contain either a string, e.g. 1+2 or an instance of another class Exercise. The Exercise class contains 2 numbers and can generate an exercise string from them.

Result class uses Pydantic (also see footnote) to validate the input as a str or Exercise types, so I must declare a Union of types:

exercise: Union[str, Exercise]

At the end I want future users of my code to get autocomplete suggestions that treat the property exercise as str only and ignore the Exercise type.

In the screenshot you can see that the autocomplete window has suggestions for both str and Exercise types.



  • In the example I am using Pydantic to define Exercise(BaseModel) to make my point, but you can use the other definition of Exercise, which is commented out.


from typing import Union

from pydantic import BaseModel

# class Exercise:
#     def __init__(self, num1: int, num2: int):
#         self.num1: int = num1
#         self.num2: int = num2
#     def convert_to_str(self) -> str:
#         return f'{self.num1} + {self.num2}'

class Exercise(BaseModel):
    num1: int
    num2: int

    def convert_to_str(self) -> str:
        return f'{self.num1} + {self.num2}'

class Result(BaseModel):
    exercise: Union[str, Exercise]

    def convert_to_str(self):
        if isinstance(self.exercise, Exercise):
            self.exercise = self.exercise.convert_to_str()

if __name__ == '__main__':
    exercise_example: Exercise = Exercise(num1=1, num2=2)
    result1: Result = Result(exercise=exercise_example)


Comment actions Permalink
Official comment


This is a limitation of PyCharm type checker, unfortunately. Here's a request to improve it

Please vote.

Comment actions Permalink

Thanks Andrey. 


Please sign in to leave a comment.