The implementation of the get method at sessions.py is the following
# sessions.py
class Session:
...
def get(self, url, **kwargs):
r"""Sends a GET request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return self.request('GET', url, **kwargs)
So it uses the request method for its implementation, setting GET as a request method. Overriding the arguments of the request method changes only the typing behavior of the request method.
The base implementation allows any keyword argument as it defines **kwargs, but it also has an interface declared at the sessions.pyi file. .pyi files are called stub files and are used to separate implementation from typing interfaces. If the implementation does not contain any typing hints, the IDE looks at the .pyi declaration.
As we can see, all possible keyword arguments of the get method are declared at sessions.pyi.
# sessions.pyi
class Session:
...
def get(
self,
url: Text | bytes,
params: _Params | None = ...,
data: Any | None = ...,
headers: Any | None = ...,
cookies: Any | None = ...,
files: Any | None = ...,
auth: Any | None = ...,
timeout: Any | None = ...,
allow_redirects: bool = ...,
proxies: Any | None = ...,
hooks: Any | None = ...,
stream: Any | None = ...,
verify: Any | None = ...,
cert: Any | None = ...,
json: Any | None = ...,
) -> Response: ...
The issue appeared as the interface did not contain the name keyword argument.
So what is happening when we call the get method on the object of the NamePrintingSession?
- As the
NamePrintingSession does not implement the get method, it will call the get method implementation of the Session class.
- As the
get implementation at sessions.py does not contain typing hints, the IDE looks for .pyi file of the sessions.py, which should be sessions.pyi, and if any interface of get is found, then uses it. If the get method did not have an interface, any keyword arguments would be allowed because of the **kwargs of the base implementation.
To fix that issue, you can customize the implementation of the get method for the NamePrintingSession class. But this solution is not optimal if the name argument should not affect the request behavior. In other words, if the name is used only in the request method, then there is no reason to override the rest methods like
# customSession.py
import requests
class NamePrintingSession(requests.Session):
def request(self, method, url, name=None, **kwargs):
print(name)
return super().request(method, url, **kwargs)
def get(self, url, name=None, **kwargs):
print(name)
super().get(url, **kwargs)
Another solution is that you can create a stub file and declare a new interface for your NamePrintingSession class.
Suppose the file that contains the NamePrintingSession class is called customSession.py. Then you should create a file with the name customSession.pyi in the same directory.
# customSession.pyi
import requests
class NamePrintingSession(requests.Session):
def request(self, method, url, name=None, **kwargs):
...
def get(self, url, name=None, **kwargs):
...