-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Conditional import of StringIO from StringIO or cStringIO doesn't work #1169
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
Comments
I've seen cases like this as well (in the Python standard library). When we have a conditional definition where one of the alternatives is a function and the other alternative is a class, we should probably generate a callable type as the result that isn't a type object. If the signatures are different, as in this case, we should try to calculate the most general signature that is compatible with both definitions. |
Duplicate of #1107. |
related to this, how do i properly annotate this code so that mypy checks these types? Right now mypy does not even complain if I remove the Union, BinaryIO, Optional. def writeln(msg: str, file: Optional[Union[str, BinaryIO]] = None) -> None:
msg += '\n'
print(msg)
if file:
if isinstance(file, (str, six.text_type)):
with open(file, mode='a') as f:
f.write(msg)
elif isinstance(file, six.StringIO):
file.write(msg)
else:
raise Exception('bad file string(IO) {} with type {}'.format(file, type(file)))
return |
@denfromufa Your problem doesn't look directly related to this issue. You can ask on Gitter or create a new issue. |
This code:
doesn't work: it complains
error: Name 'StringIO' already defined
on the second import. I think this is because in cStringIO, the function StringIO is an overloaded function (returning different types with and without argument) while in StringIO.py there is a class StringIO.I'm not sure how to fix this.
The text was updated successfully, but these errors were encountered: