Skip to content
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

Union runtime check fails for xml.etree.cElementTree.Element #229

Closed
OrenLeaffer opened this issue Jun 3, 2016 · 7 comments
Closed

Union runtime check fails for xml.etree.cElementTree.Element #229

OrenLeaffer opened this issue Jun 3, 2016 · 7 comments
Milestone

Comments

@OrenLeaffer
Copy link

from typing import *
from xml.etree.cElementTree import Element
T = Union[Element, Optional[str]]

Under python 2, this fails at runtime, but it works for static type checking. Currently the best workaround seems to be to define T in a if False: block, which allows the static check to pass without causing runtime errors.

@gvanrossum
Copy link
Member

What error do you get?

@refi64
Copy link

refi64 commented Jun 3, 2016

Traceback (most recent call last):
  File "y.py", line 3, in <module>
    T = Union[Element, Optional[str]]
  File "/usr/local/lib/python2.7/dist-packages/typing.py", line 549, in __getitem__
    dict(self.__dict__), parameters)
  File "/usr/local/lib/python2.7/dist-packages/typing.py", line 479, in __new__
    params.append(_type_check(p, msg))
  File "/usr/local/lib/python2.7/dist-packages/typing.py", line 298, in _type_check
    raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Union[arg, ...]: each arg must be a type. Got <built-in function Element>.

@ddfisher
Copy link

ddfisher commented Jun 3, 2016

I don't think this is a Python 2 issue in particular, though -- it just so happens that only the Python 2 version of xml.etree.cElementTree.Element exhibits this problem.

@refi64
Copy link

refi64 commented Jun 3, 2016

Probably a horrible idea...but what if typing._type_check just has a list of "special cases", a list of functions that look like they're types. Then it could do an identity check (NOT equality) for each of them.

@gvanrossum
Copy link
Member

Can you see if this is already fixed with the latest python2/typing.py from the githubcom/python/typing repo?

@OrenLeaffer
Copy link
Author

I tried the file you mentioned and ran into the same issue.

In the particular use I'm dealing with, I am able to get away with importing Element from ElementTree instead of cElementTree, which seems like a plausible workaround for this case. Though it is technically incorrect, since the returned object is not actually an instance of ElementTree.Element.

The only way I can figure out how to access the actual type of the object returned by xml.etree.cElementTree.Element() is by calling type() on the returned object.

@gvanrossum
Copy link
Member

Probably _type_check() should just allow anything that's callable. It's just meant to quickly diagnose some obviously bad calls like Union[1, ''], and a false positive is much more annoying than a false negative (which a real type checker presumably would catch).

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

4 participants