F
F
Finnick is healthy2020-07-23 11:43:32
Python
Finnick is healthy, 2020-07-23 11:43:32

How to display auto-tips on type typing.List[typing.Union] for any list item?

I need to make it so that when entering a dot, paycharm offers me methods not only related to dict, but also to str.
For example, to have both join() and items() in the tooltip

class A(BaseModel):
    items: List[dict] = Field(None)


class B(BaseModel):
    items: List[str] = Field(None)


class C(BaseModel):
    response: Union[A, B] = Field(None)


c = C(**{'response': {'items': [1, 2, 3]}})


K2iFW.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-07-23
@trapwalker

You want weird.
Namely, for some reason you want to make code that is obviously unsafe, and at the same time you want the IDE to help you with this. This is what I mean: the case that you brought here is an anti-pattern; it would be strange to expect such a specific handling of this situation from the IDE.
In what, actually, a problem.
The method you call on an object should not depend on what data you have as input, otherwise your code becomes unsafe and with the same data set, an error is possible due to the absence of the called method.
Such things need to be parsed explicitly in the code.
For example, you can explicitly check the variable for belonging to a type and the IDE will take this into account when making a hint:

resp = c.response
if isinstance(resp, A):
    resp.items[0].

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question