Skip to content

namedtuple should support "module" parameter added in Python 3.6 #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gvanrossum opened this issue Sep 12, 2016 · 5 comments
Closed

namedtuple should support "module" parameter added in Python 3.6 #533

gvanrossum opened this issue Sep 12, 2016 · 5 comments

Comments

@gvanrossum
Copy link
Member

See http://bugs.python.org/issue17941

@elazarg
Copy link
Contributor

elazarg commented Sep 13, 2016

The current definition is wrong. From collections.__init__.pyi:

# namedtuple is special-cased in the type checker; the initializer is ignored.
namedtuple = object()

Well, no. namedtuple is special-cased in some type checkers, but if I write my own, I might defer this specialization indefinitely, and in the meantime treat it as a simple function call, that returns type or Type or something.

Even when it is special cased by the type checker, for uniformity it might want to use the actual signature; currently this is done ad-hoc and incorrectly in mypy:

$ cat > tmp.py
from collections import namedtuple
T = namedtuple('T', 'a b', verbose=True, rename=False)
$ mypy tmp.py
tmp.py:2: error: Too many arguments for namedtuple()
tmp.py:2: error: "object" not callable

The last error message does not happen with correct signature in collections.__init__.pyi, and mistakes result in the usual, clear error messages.

(It might even be helpful to add a pseudo-implementation, so that type checkers can simply inline and continue, getting rough approximation of the actual type. This is not like adding implementation of arbitrary functions, as this is a type constructor with trivial control flow)

@gvanrossum
Copy link
Member Author

I think this would be reasonable to put in the stub:

# namedtuple is special-cased in the type checker; the initializer is ignored.
def namedtuple(typename: str, field_names: Union[str, Iterable[Any]], *,
               verbose: bool = False, rename: bool = False, module: bool = None) -> Type[tuple]: ...

but we also need to add support for the keyword args to mypy. Can you file a separate bug for that? I'm happy to take a PR for the above.

@elazarg
Copy link
Contributor

elazarg commented Sep 13, 2016

I assume you mean module: str? And why put the initializers instead of ellipsis?


While we're at it, perhaps we should add

def cast(tp: Type[_T], obj: Any) -> _T: ...

def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...

But I don't know if that's valid. cast[T](obj) and NewType[T](name) would have made more sense :(

@gvanrossum
Copy link
Member Author

Just send a PR already. :-)

On Tue, Sep 13, 2016 at 9:51 AM, Elazar [email protected] wrote:

I assume you mean module: str? And why put the initializers instead of

ellipsis?

While we're at it, perhaps we should add

def cast(tp: Type[_T], obj: Any) -> _T: ...

def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...

But I don't know if that's valid. castT and NewTypeT would
have made more sense :(


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#533 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACwrMoVBWs_aqhHCH7UBhfmHysn9kC1bks5qptR5gaJpZM4J6qFv
.

--Guido van Rossum (python.org/~guido)

@gvanrossum
Copy link
Member Author

Fixed by #541.

(If you want to do cast() and NewType() too, just submit another PR.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants