mypy error: Incompatible types in assignment (expression has type “str”, variable has type “list[str]”)

I am using mypy for linting and I am getting the following error:
Incompatible types in assignment (expression has type "str", variable has type "list[str]").

Full Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def extract_version(path: str, distro: str) -> str:
with open(path, 'r') as f:
version_info = f.readline().strip().split("-")
app_version = version_info[-1].split("_")
if distro not in app_version:
app_version = ".".join(app_version[1:])
return app_version
elif distro == app_version[0]:
return "Unknown"
elif distro in app_version:
app_version.pop(0)
distro_index = app_version.index(distro)
app_version = ".".join(app_version[:distro_index])
return app_version
return "Unknown"
</code>
<code>def extract_version(path: str, distro: str) -> str: with open(path, 'r') as f: version_info = f.readline().strip().split("-") app_version = version_info[-1].split("_") if distro not in app_version: app_version = ".".join(app_version[1:]) return app_version elif distro == app_version[0]: return "Unknown" elif distro in app_version: app_version.pop(0) distro_index = app_version.index(distro) app_version = ".".join(app_version[:distro_index]) return app_version return "Unknown" </code>
def extract_version(path: str, distro: str) -> str:
    with open(path, 'r') as f:
        version_info = f.readline().strip().split("-")
        app_version = version_info[-1].split("_")

    if distro not in app_version:
        app_version = ".".join(app_version[1:])
        return app_version
    elif distro == app_version[0]:
        return "Unknown"
    elif distro in app_version:
        app_version.pop(0)
        distro_index = app_version.index(distro)
        app_version = ".".join(app_version[:distro_index])
        return app_version
        
    return "Unknown"

The offending lines are:

app_version = ".".join(app_version[1:])

app_version = ".".join(app_version[:distro_index])

Unfortunately, I can’t disable mypy. Is there a workaround for this?

You’re getting the error because you initially declare app_version as a list of strings, and then assign a string to it. This is the kind of thing that can easily lead to a confusing bug where you lose track of what type the variable has at different parts of the function, and end up with a runtime error. The reason you use mypy is to avoid stumbling into exactly this type of mistake.

An easy fix would be to eliminate the superfluous re-assignment before you return the final value:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def extract_version(path: str, distro: str) -> str:
with open(path, 'r') as f:
version_info = f.readline().strip().split("-")
app_version = version_info[-1].split("_")
if distro not in app_version:
return ".".join(app_version[1:])
elif distro == app_version[0]:
return "Unknown"
elif distro in app_version:
app_version.pop(0)
distro_index = app_version.index(distro)
return ".".join(app_version[:distro_index])
else:
return "Unknown"
</code>
<code>def extract_version(path: str, distro: str) -> str: with open(path, 'r') as f: version_info = f.readline().strip().split("-") app_version = version_info[-1].split("_") if distro not in app_version: return ".".join(app_version[1:]) elif distro == app_version[0]: return "Unknown" elif distro in app_version: app_version.pop(0) distro_index = app_version.index(distro) return ".".join(app_version[:distro_index]) else: return "Unknown" </code>
def extract_version(path: str, distro: str) -> str:
    with open(path, 'r') as f:
        version_info = f.readline().strip().split("-")
        app_version = version_info[-1].split("_")

    if distro not in app_version:
        return ".".join(app_version[1:])
    elif distro == app_version[0]:
        return "Unknown"
    elif distro in app_version:
        app_version.pop(0)
        distro_index = app_version.index(distro)
        return ".".join(app_version[:distro_index])
    else:      
        return "Unknown"

This way app_version is always a list[str] no matter what part of the function you’re looking at.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật