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

Update _can_assign_attr to return False for builtins.object #946

Conversation

nelfin
Copy link
Contributor

@nelfin nelfin commented Apr 13, 2021

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

object() does not have a __dict__ and so does not support attribute assignment.

Type of Changes

Type
🐛 Bug fix

Related Issue

Closes #945

@nelfin nelfin force-pushed the fix/945-attributes-should-not-be-set-on-object branch from 681c7a9 to da10597 Compare April 13, 2021 22:47
@nelfin nelfin marked this pull request as ready for review April 13, 2021 23:14
@nelfin nelfin changed the title WIP: fix _can_assign_attr Update _can_assign_attr to return False for builtins.object Apr 13, 2021
@nelfin nelfin force-pushed the fix/945-attributes-should-not-be-set-on-object branch from 80f957a to c6e0435 Compare April 21, 2021 03:27
@nelfin
Copy link
Contributor Author

nelfin commented Apr 21, 2021

(updated for autoflake deconflicting)

@hippo91 hippo91 self-assigned this Apr 21, 2021
Copy link
Contributor

@hippo91 hippo91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix @nelfin!
It indeed fixes a lot of issues but in the case of trying to set an attribute to object pylint does not complain whereas it does when trying to assign an attribute not in the slots collection.
Maybe we should add such check in pylint. @nelfin @cdce8p @Pierre-Sassoulas what do you think about it?

Comment on lines +11 to +15
Closes #945
Closes PyCQA/pylint#4232
Closes PyCQA/pylint#4221
Closes PyCQA/pylint#3970
Closes PyCQA/pylint#3595
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIce set of bug fixed! Congrats! 👍

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great change ! Yes it seems like we could extend the pylint check in another MR.

@cdce8p
Copy link
Member

cdce8p commented Apr 21, 2021

Looks like a reasonable change 👍🏻
@nelfin I reran the tests for Home Assistant again and it seems like most not-callable errors are fixed. (I used https://github.com/nelfin/astroid/tree/fix/942-merge). Unfortunately a new one also appeared. I haven't taken a closer look yet to see what exactly is causing the issue, so it could be a pylint issue as well.

CONST = "my_constant"

def func():
    domain_data = {
        CONST: None,
    }
    domain_data[CONST] = lambda: print("Callback called")

    def _inner_func():
        domain_data[CONST]()  # false-positve `not-callable`

    _inner_func()
func()

@nelfin
Copy link
Contributor Author

nelfin commented Apr 21, 2021

@cdce8p: I was able to make a simplified version of your example fail on pylint tag 2.7.4 and astroid 2.5.3 so this issue appears to be underlying.

# pylint: disable=missing-docstring

data = {
    'abc': None,
}
data['abc'] = lambda: print("Callback called")
data['abc']()  # false-positive `not-callable`
$ python -m pylint --version
pylint 2.7.4
astroid 2.5.3
Python 3.9.0 (default, Nov 30 2020, 15:33:28) 
[GCC 5.4.0 20160609]

nelfin added a commit to nelfin/pylint that referenced this pull request Apr 21, 2021
@nelfin
Copy link
Contributor Author

nelfin commented Apr 21, 2021

FYI @cdce8p: I've opened pylint-dev/pylint#4387 to capture some repros of the issues that we've seen during the debugging of this and #927 that I haven't otherwise been able to fix

@cdce8p cdce8p mentioned this pull request Apr 22, 2021
nelfin added 4 commits May 11, 2021 10:41
Ref pylint-dev#945, pylint#4232, pylint#3970, pylint#3595. Various interactions
had been previously noticed with the typing/collections modules and
methods named prev/next on objects. This was due to inference setting
these values as instance attributes on the builtin object class due to
a sentinel object and an incorrectly inferred return value in the
OrderedDict definition. This change updates _can_assign_attr (and the
resulting delayed_assattr behaviour) to ignore attempts to assign to
object() since these would fail.
@nelfin nelfin force-pushed the fix/945-attributes-should-not-be-set-on-object branch from c6e0435 to 96d0496 Compare May 11, 2021 00:46
@nelfin
Copy link
Contributor Author

nelfin commented May 11, 2021

Rebased and deconflicted changelog

@Pierre-Sassoulas
Copy link
Member

Is this mergeable now @hippo91 ?

@hippo91 hippo91 merged commit cd8b6a4 into pylint-dev:master May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Delayed attribute assignment to object() may cause incorrect inference of instance attributes
4 participants