Skip to content

Issues with new Union Syntax #8119

Closed
@cdce8p

Description

@cdce8p

Bug description

Home Assistant dropped Python 3.9 this week. While upgrading all runtime annotations to the new union syntax, I discovered some issues with pylint. It seems astroid doesn't know how to handle these.

# pylint: disable=missing-docstring,too-few-public-methods,line-too-long,useless-suppression
from __future__ import annotations

from typing import Generic, TypeVar

T = TypeVar("T")


class Coordinator(Generic[T]):
    def __init__(self, update_interval=None) -> None:
        self.update_interval = update_interval


class Child(Coordinator[int | str]):  # <-- 'int | str' here seems to be the issue
    def __init__(self) -> None:
        Coordinator.__init__(self, update_interval=2)  # non-parent-init-called

    def _async_update_data(self):
        assert self.update_interval  # access-member-before-definition
        self.update_interval = 1  # attribute-defined-outside-init

Configuration

[MAIN]
load-plugins=
    pylint.extensions.code_style,
    pylint.extensions.typing,
py-version=3.10

[TYPING]
runtime-typing=no

Command used

pylint test.py

Pylint output

test.py:16:8: W0233: __init__ method from a non direct base class 'Coordinator' is called (non-parent-init-called)
test.py:19:15: E0203: Access to member 'update_interval' before its definition line 20 (access-member-before-definition)
test.py:20:8: W0201: Attribute 'update_interval' defined outside __init__ (attribute-defined-outside-init)

Expected behavior

No errors

Pylint version

pylint 2.16.0b1
astroid 2.14.0-dev0
Python 3.11.1

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions