Overriding type hint for an attribute of a class from 3rd party library

1.2k Views Asked by At

I have a 3rd-party web-framework which has type hints in some of its code. I can not control the code of the said framework

Suppose I have following piece

from framework import web
...


class UserPosts(web.WebView):
    async def get(self):
        user_id = await self.request.user_dict['id']
        ...
    

web.WebView class has type hints in its code and request attribute is marked as an instance of a specific class which doesn't have user_dict attr in definition because it is a dict that my custom made middleware dynamically attaches to the request instance.

I want to provide type hint for user_dict but I don't understand how this can be approached.

Can someone help me with a tip about the right direction?

Thanks.

1

There are 1 best solutions below

3
On

Define a new class, and override the ctor within that class:

class MyWebView(web.WebView):

    def __init__(self, ...):
        super().__init__( ... )

So far nothing has changed.

Now adjust the constructor so it uses proper type hints, add logging and parameter validation to taste, the world is your oyster!