Skip to content

New semantic analyzer: conditionally defined functions #6318

Closed

Description

@JukkaL

Support conditionally defined functions was dropped at some point when implementing the early versions of the new semantic analyzer. It's probably worth implementing this from scratch since the old implementation was pretty messy.

This issue applies to functions where multiple definitions are considered reachable by mypy. Cases involving platform and Python version checks where only one implementation is reachable already work. Example where this applies:

if cond():  # Condition determined at runtime!
    def foo() -> None:
        ... # something; will be checked
else:
    def foo() -> None:
        ... # something else; will also be checked

Activity

  1. ilevkivskyi commented on Feb 4, 2019

    @ilevkivskyi
    Member

    A clean solution for this may be non-trivial. In general we might need to switch from node.node to node.nodes, this will also allow us to support conditional definition of variables, conditional imports etc., but this is a big change. It seems to me there is a whole bunch of issues around conditional definitions and it would make sense to discuss/fix them together.

  2. JukkaL commented on Feb 4, 2019

    @JukkaL
    CollaboratorAuthor

    I was thinking of just cleaning up the implementation a bit now, without changing their behavior or general implementation strategy.

  3. ilevkivskyi commented on Feb 17, 2019

    @ilevkivskyi
    Member

    We also will need to support something like this:

    if cond():
        func: Callable[[str], Sequence[int]] = ...
    else:
        def func(spec: str) -> Sequence[int]:
            ...

    This is supported by old analyzer, and there are examples of this in our internal code (currently this causes a crash).

  4. self-assigned this
    on Feb 18, 2019
  5. added a commit that references this issue on Feb 19, 2019
    ffd8e89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions