[Python-ideas] Better Type Hinting

Stephen Hansen me+python at ixokai.io
Fri Jun 5 09:41:37 CEST 2015


On Fri, Jun 5, 2015, at 12:10 AM, Nick Coghlan wrote:
> On 5 June 2015 at 16:19, Stephen Hansen <me+python at ixokai.io> wrote:
> > On Thu, Jun 4, 2015, at 11:01 PM, Andrew Barnert via Python-ideas wrote:
> >> On Jun 4, 2015, at 22:36, Thomas Güttler <guettliml at thomas-guettler.de>
> >> wrote:
> >> >
> >> > It would be great to have better type hinting in IDEs.
> >>
> >> Is PEP 484 not sufficient for this purpose?
> >
> > It's really not.
> >
> > For one thing, PEP 484 isn't going to result in the standard library
> > being hinted all up (though I assume someone may make stubs).
> 
> Doing exactly that is a core part of the PEP 484 effort, since it's
> needed to assist in Python 2 -> 3 migrations:
> https://github.com/JukkaL/typeshed

How so? Nothing in PEP 484 addresses signatures which take *args and
**kwargs arguments. Please, correct me if I'm wrong, but my
understanding is you can uselessly specify types for a Mapping, but you
can't specify what actual keys are valid in that mapping. That's the
problem with logging's specification, its functions take "anything" and
pass on "anything", in their signature... In reality they take up two
keyword arguments -- exc_info and extra. Args really is "anything" as
its just formatted against the string. 

Unless I'm missing something, PEP484 only allows defining *types* of
specific arguments -- but this isn't about types. This is about what
arguments are valid (and then, after you have that bit of knowing, what
types come next). When used with API's that take *args and **kwargs, I
don't see how PEP484 is useful at all. 

I'm not arguing against PEP484. but it has nothing at all to do with the
specific problem mentioned here.

Dynamic API's that take "any args" and "any kwargs" are opaque things it
doesn't tell anything about. Logger.warn (and, logging.warn) is one such
API. 

On Fri, Jun 5, 2015, at 12:21 AM, Thomas Güttler wrote:
> 
> Am 05.06.2015 um 08:19 schrieb Stephen Hansen:
> > On Thu, Jun 4, 2015, at 11:01 PM, Andrew Barnert via Python-ideas wrote:
> >> On Jun 4, 2015, at 22:36, Thomas Güttler <guettliml at thomas-guettler.de>
> >> wrote:
> >>>
> >>> It would be great to have better type hinting in IDEs.
> >>
> >> Is PEP 484 not sufficient for this purpose?
> >
> > It's really not.
> >
> > For one thing, PEP 484 isn't going to result in the standard library
> > being hinted all up (though I assume someone may make stubs). But
> > really, the specific issue that the OP is running into is because of the
> > signature of logging.warn --  msg, *args, **kwargs.
> 
> I am using logger.warn() not logging.warn().

Same difference. Logging.warn is just a thin wrapper around the root
logger's warn(). They still have the same completely opaque signature,
of msg, *args, **kwargs that it passes along, which is why your IDE
can't report a useful signature that tells you that exc_info=True is
what you want. 

--S


More information about the Python-ideas mailing list