Cannot inspect members of docker client
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 1.7k
- Avg merge
- 13d 8h
- Merged PRs (30d)
- 2
Description
I was trying to inspect the modules of the client object using inspect.getmembers when I got the following error:
I am using the master branch of docker-py, but the issue exists even on the stable 5.0.3 from pypi.
import docker
from inspect import getmembers
client = docker.from_env()
m = getmembers(client)
print(m)
Error:
Traceback (most recent call last):
File "test.py", line 7, in <module>
print(m)
File "/Users/cozek/docker_api_work/docker-py/docker/models/resource.py", line 20, in __repr__
return f"<{self.__class__.__name__}: {self.short_id}>"
File "/Users/cozek/docker_api_work/docker-py/docker/models/resource.py", line 43, in short_id
return self.id[:10]
TypeError: 'NoneType' object is not subscriptable
If you print the class name of the class whose id is NoneType, it is the Swarm class.
Supposed to be fixed in 2.6.0 as written
https://docker-py.readthedocs.io/en/stable/change-log.html?highlight=id
Fixed a bug where the id member on Swarm objects wasn’t being populated.
Workaround:
# `class Model` in resources.py
@property
def short_id(self):
"""
The ID of the object, truncated to 10 characters.
"""
return self.id[:10] if self.id else self.id
Now if you print the modules using inspect.getmodules you can see
...,('swarm', <Swarm: None>),('version', <bound method DockerClient.version of <docker.client.DockerClient object at 0x7f90fd06beb0>>), ('volumes', <docker.models.volumes.VolumeCollection object at 0x7f91006c4430>)]
Should I make a PR with the workaround, or is there a deeper fix ?
Other information:
-e git+https://github.com/cozek/docker-py.git@a48a5a9647761406d66e8271f19fab7fa0c5f582#egg=docker
Python 3.8.12
Client:
Cloud integration: 1.0.17
Version: 20.10.7
API version: 1.41
Go version: go1.16.4
Git commit: f0df350
Built: Wed Jun 2 11:56:22 2021
OS/Arch: darwin/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:54:58 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.6
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc:
Version: 1.0.0-rc95
GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
docker-init:
Version: 0.19.0
GitCommit: de40ad0
OS, distribution and OS version
ProductName: macOS
ProductVersion: 11.6.1
BuildVersion: 20G224
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with docker/models/resource.py and the Model class in resources.py, then reproduce the issue with inspect.getmembers on a client created by docker.from_env(). Trace why the Swarm object's id is None and verify that inspecting the client no longer raises the reported TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100