exoscale / python-exoscale

Python bindings for the Exoscale APIs
https://exoscale.github.io/python-exoscale/
ISC License
14 stars 7 forks source link

Make API classmethods protected #4

Closed falzm closed 4 years ago

falzm commented 4 years ago

Hide API classmethod functions from the public API to avoid users being tempted to use them directly.

exo-cedric commented 4 years ago

Wouldn't a single underscore mean the methods are protected rather than private ?

falzm commented 4 years ago

Wouldn't a single underscore mean the methods are protected rather than private ?

You're right, I didn't know there was different meanings depending on the number of _s. I'll change to __.

falzm commented 4 years ago

So, turns out that when I switch previously protected methods to private nothing works anymore:

>>> import exoscale
>>> exo = exoscale.Exoscale(trace=False)
>>> gva2 = exo.compute.get_zone("ch-gva-2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/marc/Documents/Exoscale/git/python-exoscale/exoscale/api/compute.py", line 2266, in get_zone
    zones = list(self.list_zones(id=id, name=name))
  File "/Users/marc/Documents/Exoscale/git/python-exoscale/exoscale/api/compute.py", line 2246, in list_zones
    yield Zone.__from_cs(i)
AttributeError: type object 'Zone' has no attribute '_ComputeAPI__from_cs'
falzm commented 4 years ago

I thought this was a matter of convention, but turns out it seems to be actually changing the behavior of the interpreter ¯\_(ツ)_/¯

exo-cedric commented 4 years ago

So maybe Make API classmethods protected is fine given your original post ?

falzm commented 4 years ago

So maybe Make API classmethods protected is fine given your original post ?

Let's say it is ;)

tgrondier commented 4 years ago

@falzm you want double leading and trailing underscores

EDIT: I mean it's not a good idea but if you really want to hide them from most IDEs and auto completioners it's good enough.

Here is how double leading underscore work: https://docs.python.org/3/tutorial/classes.html#private-variables

falzm commented 4 years ago

@falzm you want double leading and trailing underscores

Yes you're right, after a bit of digging in this direction it would be the "right" thing to do but pragmatically the single leading underscore should be enough to signal the library users.

falzm commented 4 years ago

This article is very useful: https://dbader.org/blog/meaning-of-underscores-in-python

falzm commented 4 years ago

Don't forget to rename the commit :)

Yep, I'll fix the commit message at merge time ;)

falzm commented 4 years ago

Integration tests passed OK ✅