Skip to content

Merge {IO,Environment,Windows}Error into OSError for Python 3.3+ #1852

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

Merged
merged 3 commits into from
Feb 14, 2018

Conversation

intgr
Copy link
Contributor

@intgr intgr commented Feb 2, 2018

Starting with Python 3.3, IOError, EnvironmentError and WindowsError are
aliases for OSError, which has all their attributes.

Also, tested on Python 3.6 that OSError.filename/filename2 may also be
bytes and None, so annotated them as Any.

Reference:

if sys.version_info >= (3, 4):
filename2 = ... # type: Union[str, bytes, None]
if sys.platform == 'win32':
winerror = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a good idea to put sys.platform checks in? The winerror attribute is missing entirely on non-Windows platforms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense.

Starting with Python 3.3, IOError, EnvironmentError and WindowsError are
aliases for OSError, which has all the attributes.

Also, tested on Python 3.6 that OSerror.filename/filename2 may be bytes
and may also be None.

Reference:
* https://docs.python.org/3/library/exceptions.html#OSError
* https://www.python.org/dev/peps/pep-3151/
@srittau
Copy link
Collaborator

srittau commented Feb 2, 2018

Question for the maintainers: Can't we leave out the branch for Python < 3.3? README.md says that typeshed targets Python 2.7 and 3.3+.

@JelleZijlstra
Copy link
Member

Yes, we don't care about 3.0-3.2. (We could also seriously consider dropping support for 3.3, since that has reached EOL.)

@intgr
Copy link
Contributor Author

intgr commented Feb 3, 2018

Thanks! I got rid of the Python <3.3 check.

It still fails a test in mypy code, which uses OSError.filename. The filename is being assigned to a variable that was inferred to str but fails since the attribute is now labelled Union[str, bytes, None]. See https://github.com/python/mypy/blob/master/mypy/test/data.py#L318-L323

What's the correct fix in this case? Should I submit a PR to mypy to create a new variable in this context? Or should I revert the filename type annotation back to str? Or are there some other options?

@intgr
Copy link
Contributor Author

intgr commented Feb 4, 2018

I created a PR for Mypy here: python/mypy#4541. I'll see whether that gets merged.

@intgr
Copy link
Contributor Author

intgr commented Feb 5, 2018

Per comment from GvR, I changed the filename/filename2 type to Any instead of Union to avoid this issue. All tests pass now.

@ambv ambv merged commit d288f44 into python:master Feb 14, 2018
@ambv
Copy link
Contributor

ambv commented Feb 14, 2018

Thanks! ✨ 🍰 ✨

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

Successfully merging this pull request may close these issues.

4 participants