You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The above snippet should not raise an error. In order to achieve this we can add the __name__ attribute to the ViewSetAction. The actions are all methods so the __name__ attribute is always defined. See also below for the use case.
# rest_framework-stubs/decorators.pyiclassViewSetAction(Protocol[_View]):
detail: boolmethods: _LOWER_CASE_HTTP_VERBSurl_path: strurl_name: strkwargs: Mapping[str, Any]
mapping: MethodMapper__call__: _View__name__: str# <-- Defining the __name__ attribute here would solve the issue
Use case
When checking actions in viewset methods, we could use self.action == self.__class__.example_action.__name__ instead of self.action == "example_action" to limit the potential risk of action naming update (type checkers would detect the attribute error) and avoid the use of error-prone strings.
System information
OS: Windows
python version: 3.10
django version: 4.1
mypy version: 1.4.1
django-stubs version: 4.2.3
The text was updated successfully, but these errors were encountered:
@Sakhi29 Sorry, your comment got lost among all notifications I'm receiving. I've assigned you. In the future, there's no need to wait for assigment, just fire a PR 😄
Bug report
What's wrong
mypy doesn't recognize the
__name__
attribute of an action. See minimal example with error below:How is that should be
The above snippet should not raise an error. In order to achieve this we can add the
__name__
attribute to the ViewSetAction. The actions are all methods so the__name__
attribute is always defined. See also below for the use case.Use case
When checking actions in viewset methods, we could use
self.action == self.__class__.example_action.__name__
instead ofself.action == "example_action"
to limit the potential risk of action naming update (type checkers would detect the attribute error) and avoid the use of error-prone strings.System information
OS
: Windowspython
version: 3.10django
version: 4.1mypy
version: 1.4.1django-stubs
version: 4.2.3The text was updated successfully, but these errors were encountered: