|
71 | 71 | "\n", |
72 | 72 | "Use absolute imports for all imports, unless the target is at the same level in the hierarchy inside a `__init__.py`.\n", |
73 | 73 | "\n", |
74 | | - "### Absolute imports\n", |
| 74 | + "### Absolute Imports\n", |
75 | 75 | "\n", |
76 | 76 | "For example, in `deeptrack/features/optics.py`:\n", |
77 | 77 | "```python\n", |
|
92 | 92 | "from .sources import * # Correct: sources is a directory.\n", |
93 | 93 | "from .optics import GaussianApodization, Aberration # Correct: importing explicitly from a file.\n", |
94 | 94 | "from .optics import * # Incorrect: wildcard imports from files should be avoided.\n", |
| 95 | + "```\n", |
| 96 | + "\n", |
| 97 | + "### Imports within Classes or Functions \n", |
| 98 | + "\n", |
| 99 | + "Not all imports need to be placed at the beginning of the module.\n", |
| 100 | + "You can import libraries within classes given it will only be used within that scope and not outside the class.\n", |
| 101 | + "\n", |
| 102 | + "For example, in `deeptrack/features.py`:\n", |
| 103 | + "```python\n", |
| 104 | + "def _check_non_overlapping(\n", |
| 105 | + " self: NonOverlapping, \n", |
| 106 | + " list_of_volumes: list[np.ndarray],\n", |
| 107 | + " ) -> bool:\n", |
| 108 | + " \"\"\"\n", |
| 109 | + " ...\n", |
| 110 | + "\n", |
| 111 | + " \"\"\"\n", |
| 112 | + " # Imports used locally within the function.\n", |
| 113 | + " from skimage.morphology import isotropic_erosion, isotropic_dilation\n", |
| 114 | + " from deeptrack.augmentations import CropTight, Pad\n", |
| 115 | + " from deeptrack.optics import _get_position\n", |
95 | 116 | "```" |
96 | 117 | ] |
97 | 118 | }, |
|
117 | 138 | }, |
118 | 139 | { |
119 | 140 | "cell_type": "code", |
120 | | - "execution_count": 2, |
| 141 | + "execution_count": null, |
121 | 142 | "metadata": {}, |
122 | 143 | "outputs": [], |
123 | 144 | "source": [ |
124 | 145 | "from __future__ import annotations\n", |
125 | 146 | "\n", |
| 147 | + "\n", |
126 | 148 | "def my_function(\n", |
127 | 149 | " param1: int | float,\n", |
128 | 150 | " param2: str,\n", |
|
170 | 192 | }, |
171 | 193 | { |
172 | 194 | "cell_type": "code", |
173 | | - "execution_count": 7, |
| 195 | + "execution_count": null, |
174 | 196 | "metadata": {}, |
175 | 197 | "outputs": [], |
176 | 198 | "source": [ |
| 199 | + "from __future__ import annotations\n", |
| 200 | + "\n", |
| 201 | + "\n", |
177 | 202 | "class MyClass:\n", |
178 | 203 | " \"\"\"A one-line descriptioon of the class.\n", |
179 | 204 | "\n", |
|
228 | 253 | " attribute_1: int\n", |
229 | 254 | " attribute_2: str\n", |
230 | 255 | "\n", |
| 256 | + " #\n", |
231 | 257 | " def __init__(\n", |
232 | | - " self: 'MyClass',\n", |
| 258 | + " self: MyClass,\n", |
233 | 259 | " parameter_1: int,\n", |
234 | 260 | " parameter_2: str,\n", |
235 | 261 | " ):\n", |
|
243 | 269 | " self.attribute_2 = parameter_2\n", |
244 | 270 | "\n", |
245 | 271 | " def get_1(\n", |
246 | | - " self: 'MyClass',\n", |
| 272 | + " self: MyClass,\n", |
247 | 273 | " ) -> int:\n", |
248 | 274 | " \"\"\"...\"\"\"\n", |
249 | 275 | " \n", |
250 | 276 | " return self.attribute_1\n", |
251 | 277 | "\n", |
252 | 278 | " def get_2(\n", |
253 | | - " self: 'MyClass',\n", |
| 279 | + " self: MyClass,\n", |
254 | 280 | " ) -> str:\n", |
255 | 281 | " \"\"\"...\"\"\"\n", |
256 | 282 | " \n", |
257 | 283 | " return self.attribute_2\n", |
258 | 284 | "\n", |
259 | 285 | " def set_1(\n", |
260 | | - " self: 'MyClass',\n", |
| 286 | + " self: MyClass,\n", |
261 | 287 | " new_value: int,\n", |
262 | 288 | " ) -> None:\n", |
263 | 289 | " \"\"\"...\"\"\"\n", |
264 | 290 | " \n", |
265 | 291 | " self.attribute_1 = new_value\n", |
266 | 292 | "\n", |
267 | 293 | " def set_2(\n", |
268 | | - " self: 'MyClass',\n", |
| 294 | + " self: MyClass,\n", |
269 | 295 | " new_value: str,\n", |
270 | 296 | " ) -> None:\n", |
271 | 297 | " \"\"\"...\"\"\"\n", |
|
282 | 308 | }, |
283 | 309 | { |
284 | 310 | "cell_type": "code", |
285 | | - "execution_count": 4, |
| 311 | + "execution_count": null, |
286 | 312 | "metadata": {}, |
287 | | - "outputs": [ |
288 | | - { |
289 | | - "data": { |
290 | | - "text/plain": [ |
291 | | - "Ellipsis" |
292 | | - ] |
293 | | - }, |
294 | | - "execution_count": 4, |
295 | | - "metadata": {}, |
296 | | - "output_type": "execute_result" |
297 | | - } |
298 | | - ], |
| 313 | + "outputs": [], |
299 | 314 | "source": [ |
300 | 315 | "\"\"\"Docstring for the example.py module.\n", |
301 | 316 | "\n", |
|
0 commit comments