Skip to content

New semantic analyzer: Support idem forward aliases #6404

Closed

Description

@ilevkivskyi

This is a follow-up for #6390

This tricky corner case is used in some real code and is supported by old semantic analyzer

C = C
class C:  # type: ignore
    pass

x: C

But with new analyzer this results in Invalid type since the first definition always wins. Probably we just need to special-case this somehow.

Activity

  1. ilevkivskyi commented on Mar 14, 2019

    @ilevkivskyi
    MemberAuthor

    EDIT: The new behavior is actually correct, see below.

    Another pattern that we need to support with new semantic analyzer is this:

    class In:
        ...
    class Out:
        In = In  # recognized as variable

    this currently (both before and after assignment refactor) causes a bunch of troubles in internal code bases. Raising priority to high.

  2. ilevkivskyi commented on Mar 14, 2019

    @ilevkivskyi
    MemberAuthor

    Oh, wait, this is actually correct, by the rules non-indexed assignments at class scope are always variables, so this was previously a false negative, OK back to normal priority.

  3. ilevkivskyi commented on Jun 7, 2019

    @ilevkivskyi
    MemberAuthor

    Another repro for this issue with a weird error:

    x: C
    class C:
        pass
    C = C
    

    fails with

    main:2: error: Name 'C' already defined on line 4
    

    cc @JukkaL

  4. added
    bugmypy got something wrong
    false-positivemypy gave an error on correct code
    on Jun 12, 2019
  5. JukkaL commented on Jul 3, 2019

    @JukkaL
    Collaborator

    The examples look meaningless, since the code won't work at runtime. I wonder if the actual code where this comes up is different (involving try/except perhaps). This could also affect the way we'd fix this.

  6. ilevkivskyi commented on Jul 3, 2019

    @ilevkivskyi
    MemberAuthor

    Yes, the actual code is like

    try:
        C = C
    except NameError:
        class C:
            ...

    Also the second example works without try/except.

    I think we should fix this before the release since this is actually used in some projects.

  7. JukkaL commented on Jul 3, 2019

    @JukkaL
    Collaborator

    Yes, it would be good to make this better. I'm increasing priority.

    But even that example seems incomplete, as that will always generate a NameError. I assume it's more like this:

    try:
        ModuleNotFoundError = ModuleNotFoundError
    except NameError:
        class ModuleNotFoundError(Exception):
            ...

    The assignment will fail on some Python versions and pass on some.

  8. ilevkivskyi commented on Jul 3, 2019

    @ilevkivskyi
    MemberAuthor

    Most likely, I don't remember TBH, also aiohttp (who first reported this) used to have tons of * imports. This may be some other name they define in another module depending on sys.platform and then star import it.

  9. ilevkivskyi commented on Jul 3, 2019

    @ilevkivskyi
    MemberAuthor

    I did some digging and it looks like this happens sometimes after star imports from C acceleration modules.

  10. self-assigned this
    on Jul 3, 2019
  11. added a commit that references this issue on Jul 3, 2019
    27f8b4f
  12. added a commit that references this issue on Jul 4, 2019
    1bce150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongfalse-positivemypy gave an error on correct codepriority-0-highsemantic-analyzerProblems that happen during semantic analysis

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions