Git Product home page Git Product logo

Comments (4)

Pierre-Sassoulas avatar Pierre-Sassoulas commented on June 20, 2024

What about adding the __add__ function directly in Incrementable ?

"""https://github.com/pylint-dev/pylint/issues/8978"""

class Incrementable:
    """class with an incrementable field"""
    def __init__(self, a):
        self.a = a

    def __add__(self, other):
        return self.__iadd__(other)

    def __iadd__(self, other):
        if isinstance(other, Incrementable):
            self.a += other.a
            return self
        return NotImplemented

d = {}
d.setdefault("a", Incrementable(6) + Incrementable(7))

from pylint.

nickdrozd avatar nickdrozd commented on June 20, 2024

Following the suggestion given by the warning results in code that fails to parse:

d.setdefault("a", Incrementable(6)) += Incrementable(7)  # Use += operator
E0001: Parsing failed: ''function call' is an illegal expression for augmented assignment (<unknown>, line 14)' (syntax-error)

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on June 20, 2024

Yes, but I think the message should be modified and still raised (in order to make the code more pythonic like in my proposed snippet). Something like 'create an __add__ method in 'Increment' and use it'

from pylint.

sam-s avatar sam-s commented on June 20, 2024

What about adding the __add__ function directly in Incrementable ?

"""https://github.com/pylint-dev/pylint/issues/8978"""

class Incrementable:
    """class with an incrementable field"""
    def __init__(self, a):
        self.a = a

    def __add__(self, other):
        return self.__iadd__(other)

    def __iadd__(self, other):
        if isinstance(other, Incrementable):
            self.a += other.a
            return self
        return NotImplemented

d = {}
d.setdefault("a", Incrementable(6) + Incrementable(7))

this does not modify d["a"] if it is already present, i.e.

d = {"a":Incrementable(2)}
d.setdefault("a", Incrementable(6) + Incrementable(7))

is not the same as

d = {"a":Incrementable(2)}
d.setdefault("a", Incrementable(6)).__iadd__(Incrementable(7))

IOW, this code is NOT equivalent to the code that I posted in all cases.

from pylint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.