GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support conditional Python version and platform checks · Issue #698 · python/mypy

Support conditional Python version and platform checks #698

New issue
Copy link
New issue
Copy link
Closed
Closed
Support conditional Python version and platform checks#698
Copy link
Assignees
gvanrossum
Labels
featuretopic-pep-484

Description

@JukkaL
JukkaL
opened on May 31, 2015
Issue body actions

Mypy should support some common kinds of Python version checks and platform checks (e.g. Windows vs. Posix). We should ignore code paths that won't be run on the targeted Python version or platform. This way mypy can more effectively type check code that supports Python 2 and 3 and multiplatform code.

We first need to decide which checks to support. These examples are from PEP 484 and should be supported:

import sys

if sys.version_info[0] >= 3:
    # Python 3 specific definitions
    ...
else:
    # Python 2 specific definitions
    ...

if sys.platform == 'win32':
    # Windows specific definitions
    ...
else:
    # Posix specific definitions
    ...

When type checking code as above, always only if or the else block would be analyzed, never both, since on any given program run only one them can be evaluated (we assume that nobody does anything crazy like modifying sys.platform at runtime). We'd detect the check expressions during semantic analysis and wouldn't semantically analyze (or type check) the skipped blocks, similar to how mypy currently deals with PY2/PY3 conditions in if statements.

We already have Python 2 and Python 3 modes, and we should also implement Windows and non-Windows (Posix) modes. Initially, we can just use the platform on which the type checker is being run, but more generally this should be configurable (e.g., mypy --platform win32 ...).

Activity

  1. JukkaL
    added
    feature
    priority
    topic-pep-484
    on May 31, 2015
  2. JukkaL commented on May 31, 2015

    @JukkaL
    JukkaL
    on May 31, 2015
    CollaboratorAuthor
    More actions

    We should also support code that assigns the result of a check to a variable. Example:

    import sys
    
    PY3_OR_LATER = sys.version_info[0] >= 3
    
    if PY3_OR_LATER:
        ...
    else:
        ...
  3. ddfisher
    added this to the This milestone has been deleted milestone on Mar 2, 2016
  4. gnprice
    removed
    priority
    on Mar 2, 2016
  5. methane commented on May 18, 2016

    @methane
    methane
    on May 18, 2016
    Member
    More actions

    How about type comment?

    def quote(s):
        # if PY2:
        #     type: (AnyStr) -> AnyStr
        # else:
        #     type: (str) -> str
        return '"' + s + '"'

    or

    def quote(s):
        if PY2:
            # type: (AnyStr) -> AnyStr
            ...
        else:
            # type: (str) -> str
            ...
        return '"' + s + '"'
  6. gvanrossum commented on May 18, 2016

    @gvanrossum
    gvanrossum
    on May 18, 2016
    Member
    More actions

    Please no new proposals.

  7. rwbarton commented on May 18, 2016

    @rwbarton
    rwbarton
    on May 18, 2016
    Contributor
    More actions

    Cases like the one above could be handled already by defining a type variable or synonym MyStr conditionally on the python version and then declaring the type of quote to be (MyStr) -> MyStr.

  8. methane commented on May 18, 2016

    @methane
    methane
    on May 18, 2016
    Member
    More actions

    I'm sorry, and thanks for information.

  9. gvanrossum commented on Jul 5, 2016

    @gvanrossum
    gvanrossum
    on Jul 5, 2016
    Member
    More actions

    Note that different versions may have different signatures for the same function. This should definitely be supported.

  10. gvanrossum
    modified the milestones: This milestone has been deleted, This milestone has been deleted on Jul 7, 2016
  11. gvanrossum
    self-assigned this
    on Jul 26, 2016
  12. gvanrossum commented on Jul 26, 2016

    @gvanrossum
    gvanrossum
    on Jul 26, 2016
    Member
    More actions

    My plan:

    • implement straight checks for sys.version_info in a conditional expression
    • ditto for sys.platform, using the current platform
    • add a command-line flag to override the default platform
    • understand variables used as aliases for platform checks

    I may need help with the last item, and maybe we should update PEP 484 to clarify what's allowed.

  13. JukkaL commented on Jul 26, 2016

    @JukkaL
    JukkaL
    on Jul 26, 2016
    CollaboratorAuthor
    More actions

    Here are some thoughts:

    • Aliases can be a little tricky, as we currently don't process any aliases during the first semantic analysis pass -- and we'd probably want to process version checks during the first pass. A potential approach is to special case the detection of these aliases during the first pass, and ignore assignment statements that don't look like aliases that we are going to support.
    • As imports aren't fully processed during the first pass, you may want to just add some special cased imported values to the symbol table during the pass -- even having a special, temporary symbol table just for conditional definitions might not be terrible. For example, shallow process import sys and from sys import platform during the first pass without actually looking at the sys stub file, but skip other imports.
    • Importing conditional flags from other modules may also be a little tricky (for example, from myappconfig import PY3 or similar). Perhaps we don't need to support these initially.
    • Simple Python version checks without aliases are probably the most important use case (for python 2 and 3 compatible stubs), and focusing on this first seems reasonable.
  14. gvanrossum commented on Jul 26, 2016

    @gvanrossum
    gvanrossum
    on Jul 26, 2016 via email
    Member
    More actions
    Good to know. Are there any existing helper functions that might be useful to pick apart things like tuples of int literals or e.g. sys.version_info[:2]? (I'm not afraid to hand-code these, but if something relevant already exists I'd like to use it.)
  15. JukkaL commented on Jul 26, 2016

    @JukkaL
    JukkaL
    on Jul 26, 2016
    CollaboratorAuthor
    More actions

    There's visit_tuple_slice_helper in checkexpr.py which might be relevant.

  16. gvanrossum
    added a commit that references this issue on Jul 27, 2016

    Implement most common sys.version_info and sys.platform checks (#1942)

    0b82463
  17. gvanrossum commented on Aug 5, 2016

    @gvanrossum
    gvanrossum
    on Aug 5, 2016
    Member
    More actions

    I did the first two bullets of my plan, and filed separate issues for the last two (the last one I think we may not actually need). So I'm closing this.

  18. gvanrossum
    closed this as completedon Aug 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @gvanrossum
    gvanrossum

Labels

featuretopic-pep-484

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.