Replies: 4 comments 1 reply
-
As you surmise, this currently isn't supported. ParamSpec is similar but doesn't quite fit here. I feel like I might have seen this discussed before, but can't find anything on the typing issue tracker. |
Beta Was this translation helpful? Give feedback.
-
I wasn't able to find anything either, which is why I created this discussion post. I think this idea is reasonably cool to have something for in the typing ecosystem. Whether or not I saw someone suggest using |
Beta Was this translation helpful? Give feedback.
-
For now, you can make your own It has downsides, but it works. |
Beta Was this translation helpful? Give feedback.
-
This was also brought up on discuss: add bound to paramspec and recently in python/mypy#17119 |
Beta Was this translation helpful? Give feedback.
-
Hello o/
I have a typing question / suggestion. I wasn't sure where to put this exactly, so if this isn't the right place, please let me know. For context, I normally use Python 3.8.10 via VSCode with Pylance for linting and MyPy for type checking.
While I was working on my Python project, I ran into a situation where I needed to create a subclass of
argparse.ArgumentParser
, to override some methods on it. Additionally, I needed an extra attribute to be stored in the parser, calledself._message
. Sounded easy enough, I just needed to override__init__
with asuper
call and set the attribute to some initial value.Since I'm not changing the
__init__
's signature, the easiest way is to just use*args
and**kwargs
to forward everything to thesuper
call:This creates a big issue though. By doing this, my overridden
__init__
just lost all of it's typing information.argparse.ArgumentParser
happens to take in quite a bit of parameters of different kinds, and I just robbed myself of any linting information that'd be useful when I'd get to making an instance of it.I initially tried to use the new
ParamSpec
feature, to try and see if it'd be able to help here, but after trying a couple of different things and rereading PEP 612, it turned out that this "signature forwarding" isn't currently supported in any way.The only way to go about this right now, is to "manually" copy the entire signature. Here's how it looks like:
There's several problems with this though:
_FormatterClass
). This doesn't look so bad here, but for exampletkinter
has lots of them that can be used in the signature.exit_on_error: bool = True
argument has been added in Python 3.9), but it also affects any 3rd party library that may adjust its signature as well.So, to summarise, it would be great to have an explicit way to specify "passthrough" signature, or in any other way preserve the typing information, when a method just passes them to a
super
call. Something like:I'm assuming this isn't currently supported? And if not, isn't this something that would be quite helpful in general development that involves typing?
Beta Was this translation helpful? Give feedback.
All reactions