Skip to content

Commit 4c9a072

Browse files
nejchJohnVillalovos
authored andcommitted
docs(faq): describe and group common errors
1 parent 887852d commit 4c9a072

File tree

2 files changed

+86
-46
lines changed

2 files changed

+86
-46
lines changed

docs/api-usage.rst

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ conflict with python or python-gitlab when using them as kwargs:
171171
172172
gl.user_activities.list(query_parameters={'from': '2019-01-01'}, iterator=True) # OK
173173
174+
.. _objects:
175+
174176
Gitlab Objects
175177
==============
176178

@@ -220,21 +222,16 @@ the value on the object is accepted:
220222
issue.my_super_awesome_feature_flag = "random_value"
221223
issue.save()
222224
225+
As a dictionary
226+
---------------
227+
223228
You can get a dictionary representation copy of the Gitlab Object. Modifications made to
224229
the dictionary will have no impact on the GitLab Object.
225230

226-
* ``asdict()`` method. Returns a dictionary representation of the Gitlab object.
227-
* ``attributes`` property. Returns a dictionary representation of the Gitlab
231+
* ``asdict()`` method. Returns a dictionary representation of the Gitlab object.
232+
* ``attributes`` property. Returns a dictionary representation of the Gitlab
228233
object. Also returns any relevant parent object attributes.
229234

230-
.. note::
231-
232-
``attributes`` returns the parent object attributes that are defined in
233-
``object._from_parent_attrs``. What this can mean is that for example a ``ProjectIssue``
234-
object will have a ``project_id`` key in the dictionary returned from ``attributes`` but
235-
``asdict()`` will not.
236-
237-
238235
.. code-block:: python
239236
240237
project = gl.projects.get(1)
@@ -244,6 +241,22 @@ the dictionary will have no impact on the GitLab Object.
244241
issue = project.issues.get(1)
245242
attribute_dict = issue.attributes
246243
244+
# The following will return the same value
245+
title = issue.title
246+
title = issue.attributes["title"]
247+
248+
.. hint::
249+
250+
This can be used to access attributes that clash with python-gitlab's own methods or managers.
251+
Note that:
252+
253+
``attributes`` returns the parent object attributes that are defined in
254+
``object._from_parent_attrs``. For example, a ``ProjectIssue`` object will have a
255+
``project_id`` key in the dictionary returned from ``attributes`` but ``asdict()`` will not.
256+
257+
As JSON
258+
-------
259+
247260
You can get a JSON string represenation of the Gitlab Object. For example:
248261

249262
.. code-block:: python

docs/faq.rst

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,79 @@
22
FAQ
33
###
44

5-
I cannot edit the merge request / issue I've just retrieved
6-
It is likely that you used a ``MergeRequest``, ``GroupMergeRequest``,
7-
``Issue`` or ``GroupIssue`` object. These objects cannot be edited. But you
8-
can create a new ``ProjectMergeRequest`` or ``ProjectIssue`` object to
9-
apply changes. For example::
5+
General
6+
-------
107

11-
issue = gl.issues.list()[0]
12-
project = gl.projects.get(issue.project_id, lazy=True)
13-
editable_issue = project.issues.get(issue.iid, lazy=True)
14-
# you can now edit the object
8+
I cannot edit the merge request / issue I've just retrieved.
9+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1510

16-
See the :ref:`merge requests example <merge_requests_examples>` and the
17-
:ref:`issues examples <issues_examples>`.
11+
It is likely that you used a ``MergeRequest``, ``GroupMergeRequest``,
12+
``Issue`` or ``GroupIssue`` object. These objects cannot be edited. But you
13+
can create a new ``ProjectMergeRequest`` or ``ProjectIssue`` object to
14+
apply changes. For example::
1815

19-
.. _attribute_error_list:
16+
issue = gl.issues.list()[0]
17+
project = gl.projects.get(issue.project_id, lazy=True)
18+
editable_issue = project.issues.get(issue.iid, lazy=True)
19+
# you can now edit the object
20+
21+
See the :ref:`merge requests example <merge_requests_examples>` and the
22+
:ref:`issues examples <issues_examples>`.
2023

21-
I get an ``AttributeError`` when accessing attributes of an object retrieved via a ``list()`` call.
22-
Fetching a list of objects, doesn’t always include all attributes in the
23-
objects. To retrieve an object with all attributes use a ``get()`` call.
24+
How can I clone the repository of a project?
25+
""""""""""""""""""""""""""""""""""""""""""""
2426

25-
Example with projects::
27+
python-gitlab does not provide an API to clone a project. You have to use a
28+
git library or call the ``git`` command.
2629

27-
for projects in gl.projects.list():
28-
# Retrieve project object with all attributes
29-
project = gl.projects.get(project.id)
30+
The git URI is exposed in the ``ssh_url_to_repo`` attribute of ``Project``
31+
objects.
3032

31-
How can I clone the repository of a project?
32-
python-gitlab doesn't provide an API to clone a project. You have to use a
33-
git library or call the ``git`` command.
33+
Example::
34+
35+
import subprocess
36+
37+
project = gl.projects.create(data) # or gl.projects.get(project_id)
38+
print(project.attributes) # displays all the attributes
39+
git_url = project.ssh_url_to_repo
40+
subprocess.call(['git', 'clone', git_url])
41+
42+
Not all items are returned from the API
43+
"""""""""""""""""""""""""""""""""""""""
44+
45+
If you've passed ``all=True`` (or ``--all`` via the CLI) to the API and still cannot see all items returned,
46+
use ``get_all=True`` (or ``--get-all`` via the CLI) instead. See :ref:`pagination` for more details.
47+
48+
Common errors
49+
-------------
50+
51+
.. _attribute_error_list:
52+
53+
``AttributeError`` when accessing object attributes retrieved via ``list()``
54+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
55+
56+
Fetching a list of objects does not always include all attributes in the objects.
57+
To retrieve an object with all attributes, use a ``get()`` call.
58+
59+
Example with projects::
3460

35-
The git URI is exposed in the ``ssh_url_to_repo`` attribute of ``Project``
36-
objects.
61+
for projects in gl.projects.list():
62+
# Retrieve project object with all attributes
63+
project = gl.projects.get(project.id)
3764

38-
Example::
65+
``AttributeError`` when accessing attributes after ``save()`` or ``refresh()``
66+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
3967

40-
import subprocess
68+
You are most likely trying to access an attribute that was not returned
69+
by the server on the second request. Please look at the documentation in
70+
:ref:`object_attributes` to see how to avoid this.
4171

42-
project = gl.projects.create(data) # or gl.projects.get(project_id)
43-
print(project.attributes) # displays all the attributes
44-
git_url = project.ssh_url_to_repo
45-
subprocess.call(['git', 'clone', git_url])
72+
``TypeError`` when accessing object attributes
73+
""""""""""""""""""""""""""""""""""""""""""""""
4674

47-
I get an ``AttributeError`` when accessing attributes after ``save()`` or ``refresh()``.
48-
You are most likely trying to access an attribute that was not returned
49-
by the server on the second request. Please look at the documentation in
50-
:ref:`object_attributes` to see how to avoid this.
75+
When you encounter errors such as ``object is not iterable`` or ``object is not subscriptable``
76+
when trying to access object attributes returned from the server, you are most likely trying to
77+
access an attribute that is shadowed by python-gitlab's own methods or managers.
5178

52-
I passed ``all=True`` (or ``--all`` via the CLI) to the API and I still cannot see all items returned.
53-
Use ``get_all=True`` (or ``--get-all`` via the CLI). See :ref:`pagination` for more details.
79+
You can use the object's ``attributes`` dictionary to access it directly instead.
80+
See the :ref:`objects` section for more details on how attributes are exposed.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy