Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataclass_transform false positives for Container type Fields #8833

Closed
Apxdono opened this issue Jul 7, 2023 · 2 comments
Closed

dataclass_transform false positives for Container type Fields #8833

Apxdono opened this issue Jul 7, 2023 · 2 comments
Labels
dataclasses Duplicate 🐫 Duplicate of an already existing issue

Comments

@Apxdono
Copy link

Apxdono commented Jul 7, 2023

Bug description

Custom dataclass-like decorators (marked with dataclass_transform) exhibit issues similar to ones described in #4899, #7884 etc.

Whenever a dataclass "container" property is declared as field(default_factory=list|dict|etc...), pylint argues that particular methods and operations aren't applicable/available for this property.

Below example will produce 4 distinct pylint errors. The only way to resolve them is to remove = field(...) declarations.

# issue.py

import dataclasses
from dataclasses import field
from typing import Dict, List, Optional, TypeVar, dataclass_transform

_C = TypeVar("_C", bound=type)


@dataclass_transform(field_specifiers=(dataclasses.field,), kw_only_default=True)
def real_class(cls: _C) -> _C:
    data_cls = dataclasses.dataclass(cls, kw_only=True, slots=True)  # type: ignore
    return data_cls


@real_class
class Vehicle:
    model: str
    wheel_count: int = field(default=4)


@real_class
class Parking:
    parked_vehicles: List[Vehicle] = field(default_factory=list)
    spots: Dict[int, Optional[Vehicle]] = field(default_factory=dict)

    def park_vehicle(self, vehicle: Vehicle, spot: int):
        self.parked_vehicles.append(vehicle)
        self.spots[spot] = vehicle


def main():
    parking = Parking()
    parking.park_vehicle(Vehicle(model="lamborghini"), 5)
    parking.park_vehicle(Vehicle(model="prius"), 3)
    parking.park_vehicle(Vehicle(model="volkswagen"), 22)

    for spot, veh in parking.spots.items():
        if veh and veh in parking.parked_vehicles:
            print(f"Spot #{spot}: {veh.model}")


if __name__ == "__main__":
    main()

Configuration

No response

Command used

$ pylint issue.py

Pylint output

************* Module attrcheck.issue
issue.py:26:8: E1101: Instance of 'Field' has no 'append' member (no-member)
issue.py:27:8: E1137: 'self.spots' does not support item assignment (unsupported-assignment-operation)
issue.py:36:21: E1101: Instance of 'Field' has no 'items' member (no-member)
issue.py:37:26: E1135: Value 'parking.parked_vehicles' doesn't support membership test (unsupported-membership-test)

------------------------------------------------------------------
Your code has been rated at 2.31/10 (previous run: 2.31/10, +0.00)

Expected behavior

$ pylint issue.py


Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Pylint version

python = 3.7
pylint = 2.16.2

or

python = 3.11
pylint 2.17.4
astroid 2.15.5

OS / Environment

Archlinux 6.4.1-zen2-1-zen

Additional dependencies

No response

@Apxdono Apxdono added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 7, 2023
@Apxdono
Copy link
Author

Apxdono commented Jul 10, 2023

Browsed a bit more and I see that this issue more or less duplicates #7437.
IMO this issue has proper "run and drive" example code that reproduces the issue.
Will leave open for maintainers to decide.

@Pierre-Sassoulas
Copy link
Member

Thank you for identifying a duplicate, I copy paster your example in the other issue :)

@Pierre-Sassoulas Pierre-Sassoulas closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
@Pierre-Sassoulas Pierre-Sassoulas added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dataclasses Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants