Skip to content

support platform and version checks in conditional expressions #5671

Closed

Description

@benjaminp

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.

Activity

  1. emmatyping commented on Sep 25, 2018

    @emmatyping
    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.

  2. benjaminp commented on Sep 25, 2018

    @benjaminp
    Author

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

  3. emmatyping commented on Sep 25, 2018

    @emmatyping
    Member

    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.

  4. gvanrossum commented on Sep 25, 2018

    @gvanrossum
    Member

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

  5. ilevkivskyi commented on Sep 26, 2018

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions