Skip to content

fix(types): prevent __dir__ in RestObject from producing duplicates #1383

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

Merged
merged 2 commits into from
Apr 21, 2021

Conversation

spyoungtech
Copy link
Contributor

@spyoungtech spyoungtech commented Mar 23, 2021

Fixes a small issue with the feature added in #1072 for __dir__ to display rest object attributes.

In at least one case, classes have overlapping attributes with the attributes listed in .attributes. For example, the Group object has its own projects property for the GroupProjectManager. This shadows the name that would normally access .attributes['projects']. This causes dir(some_group), as currently implemented, to list projects twice.

While this is unlikely to be problematic, it's ideal if duplicate attributes do not appear in the response for __dir__. This PR ensures that duplicates are not produced by the __dir__ method.

Instead of adding the list of attributes, we use the | operator to produce a set with the union of all the elements, ensuring there are no duplicates.

Copy link
Member

@JohnVillalovos JohnVillalovos left a comment

Choose a reason for hiding this comment

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

I think this needs a unit test.

In my quick testing of this idea it doesn't seem to work.

>>> a = [1,2] | [3,4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'list' and 'list'

@nejch
Copy link
Member

nejch commented Apr 20, 2021

@JohnVillalovos I think this would work, because dict keys() returns a set-like object https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects.
Although true, if you like @spyoungtech, maybe a quick test against the original issue would be great :)

@JohnVillalovos
Copy link
Member

@JohnVillalovos I think this would work, because dict keys() returns a set-like object https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects.
Although true, if you like @spyoungtech, maybe a quick test against the original issue would be great :)

Ah correct. I thought both were lists.

@@ -132,7 +132,7 @@ def __ne__(self, other: object) -> bool:
return super(RESTObject, self) != other

def __dir__(self):
return super(RESTObject, self).__dir__() + list(self.attributes)
return super(RESTObject, self).__dir__() | self.attributes.keys()
Copy link
Member

Choose a reason for hiding this comment

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

Can the result be converted to a list? As that is what it returned before.

    return list(super(RESTObject, self).__dir__() | self.attributes.keys())

Copy link
Contributor Author

@spyoungtech spyoungtech Apr 21, 2021

Choose a reason for hiding this comment

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

The __dir__ method can return any sequence and will be converted into a list and sorted automatically. For example, if you return a set or any other sequence, dir(obj) will always be a list.

class MyObj:
    def __dir__(self):
        return {'abc', 'xyz', 'def',}
obj = MyObj()
dir(obj)
['abc', 'def', 'xyz']

If you like we can convert it in the method directly, if for nothing else, to be more explicit, though the result is the same in either case.

Suggested change
return super(RESTObject, self).__dir__() | self.attributes.keys()
return list(super(RESTObject, self).__dir__() | self.attributes.keys())

Copy link
Member

Choose a reason for hiding this comment

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

I (for selfish reasons) would like it to return a list. As I am going to be working on adding type-hints to that code in the future 😊 But not necessary.

Copy link
Member

Choose a reason for hiding this comment

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

And not necessary, because I think I will be adding a type-hint for the return value of Iterable[something], most likely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me. Feel free to apply the above suggestion (or not)

Looking forward to type hinting!

Worth mentioning that it's usually not necessary to provide type annotations or type hints for certain magic methods, since their return type is implied because there's no way to implement them to return a different type.

For example, the return type for methods like __str__, __repr__, __len__, __dir__, are known to IDEs and type checkers like MyPy automatically because the returned type must be valid, otherwise a TypeError is raised.

@spyoungtech
Copy link
Contributor Author

Went ahead and added test case @nejch

Confirmed it fails on the main branch, but passes with the changes this PR adds.

@nejch
Copy link
Member

nejch commented Apr 21, 2021

Went ahead and added test case @nejch

Confirmed it fails on the main branch, but passes with the changes this PR adds.

Great, thanks @spyoungtech!

@nejch nejch merged commit 60c5fd8 into python-gitlab:master Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants