Skip to content

support platform and version checks in conditional expressions #5671

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
benjaminp opened this issue Sep 25, 2018 · 5 comments
Closed

support platform and version checks in conditional expressions #5671

benjaminp opened this issue Sep 25, 2018 · 5 comments
Labels

Comments

@benjaminp
Copy link

benjaminp commented Sep 25, 2018

I wanted to write os.environb if sys.version_info[0] == 3 else os.environ to obtain a dictionary of bytes environmental variables. However, this fails to typecheck on Python 2 because mypy doesn't recognize the version in conditional expressions.

@emmatyping
Copy link
Member

We do support platform and version checks, but only sys.platform and sys.version_info. Considering six is used a fair amount for Python2/3 straddling code, I think we should also support it.

@benjaminp
Copy link
Author

benjaminp commented Sep 25, 2018

Ah, sorry, I didn't mean this to be about six. There's the same problem with sys.version_info[0] == 3.

@emmatyping
Copy link
Member

emmatyping commented Sep 25, 2018

Ah, interesting, with this:

import sys
import os
reveal_type(os.environb if sys.version_info > (3, 0, 0) else os.environ)

This reveals builtins.object on Python 3 and gives an attribute error on Python 2 (since os.environb doesn't exist). This does indeed seem to be a bug.

@emmatyping emmatyping added bug mypy got something wrong priority-0-high and removed feature priority-1-normal labels Sep 25, 2018
@gvanrossum
Copy link
Member

(OT: six.PY3 support is easy: just pass --always-true=PY3.)

@ilevkivskyi
Copy link
Member

This is a duplicate of #5550.

I will raise the priority of that issue (but still not high probably, since there is a simple workaround).

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

No branches or pull requests

4 participants