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 — Incorrect union narrowing in chained conditionals · Issue #12208 · python/mypy

Incorrect union narrowing in chained conditionals #12208

New issue
Copy link
New issue
Copy link
Closed
Closed
Incorrect union narrowing in chained conditionals#12208
Copy link
Labels
bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

Description

@JaviFuentes94
JaviFuentes94
opened on Feb 18, 2022
Issue body actions

Bug Report
Hey there!

I have recently encountered this case:

class Animal:
    name: Literal["animal"]
    n_legs: int

class Plant:
    name: Literal["plant"]

def get_n_legs(item: Union[Animal, Plant]):
    if item.name == "animal" and item.n_legs > 6:    # This fails mypy with union-attr for n_legs in Plant
        return "Many" 

Expected Behavior

Mypy should be able to narrow down chained conditionals as presented. It seems that pylance has no problems with it, for example.

Activity

  1. JaviFuentes94
    added
    bugmypy got something wrong
    on Feb 18, 2022
  2. erictraut commented on Feb 19, 2022

    @erictraut
    erictraut
    on Feb 19, 2022
    More actions

    Which version of mypy are you using? I'm not able to repro this error with the latest published version.

  3. JelleZijlstra commented on Mar 19, 2022

    @JelleZijlstra
    JelleZijlstra
    on Mar 19, 2022
    Member
    More actions

    Doesn't repro for me either: https://mypy-play.net/?mypy=latest&python=3.10&gist=e2c735beb847d6da4d53477050f3a119

  4. JelleZijlstra
    closed this as completedon Mar 19, 2022
  5. JelleZijlstra
    added
    topic-type-narrowingConditional type narrowing / binder
    on Mar 19, 2022
  6. JaviFuentes94 commented on Mar 22, 2022

    @JaviFuentes94
    JaviFuentes94
    on Mar 22, 2022
    Author
    More actions

    Hello again, sorry I made a mistake while testing my minimal example. The problem comes with a Literal that is an enum field:

    from typing_extensions import Literal
    from typing import Union, Dict
    import enum
    
    class Types(str, enum.Enum):
        animal = "animal"
        plant = "plant"
    
    
    class Animal:
        name: Literal[Types.animal]
        n_legs: int
    
    class Plant:
        name: Literal[Types.plant]
    
    def get_n_legs(item: Union[Animal, Plant]):
        if item.name == Types.animal and item.n_legs > 6:   
            return "Many" 
    

    Here you can reproduce it: https://mypy-play.net/?mypy=latest&python=3.10&gist=dfcdd1d12e53c80d0e58ba5ed8750ccf

  7. JelleZijlstra
    reopened this on Mar 22, 2022
  8. mikaelarguedas commented on Mar 4, 2023

    @mikaelarguedas
    mikaelarguedas
    on Mar 4, 2023
    More actions

    Hey there,

    I believe I ran into another occurrence of this. It looks like the type has not been narrowed by the first if statement.
    Happens with latest mypy on Python3.8.
    Trying it on https://mypy-play.net looks to happen on more recent versions of Python as well.

    Reproducible example: https://mypy-play.net/?mypy=latest&python=3.11&flags=verbose&gist=07d004a22a748059c834ff42d690c2ed

    from typing import Optional, Union
    
    
    class RangeVals:
        """Hold a parameter value range."""
    
        def __init__(
            self,
            min_val: Union[int, float],
            max_val: Union[int, float],
        ) -> None:
            self.min_val = min_val
            self.max_val = max_val
    
    
    def functionn(default_val: Union[str, float, int], rangeval: RangeVals):
        if type(default_val) != type(rangeval.min_val):
            raise TypeError("foo")
        if default_val < rangeval.min_val:
            raise ValueError("bar")
    
    
    functionn("abc", RangeVals(min_val=3, max_val=10))
  9. erictraut commented on Aug 13, 2023

    @erictraut
    erictraut
    on Aug 13, 2023
    More actions

    The original bug in this issue appears to have been fixed in mypy 1.4.

    @mikaelarguedas, your issue is unrelated to OP's bug. The behavior in your sample is expected behavior in mypy.

  10. hauntsaninja commented on Aug 13, 2023

    @hauntsaninja
    hauntsaninja
    on Aug 13, 2023
    Collaborator
    More actions

    I fixed this in #11521

  11. hauntsaninja
    closed this as completedon Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — 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.