| Conditions | 22 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like describe_aliases() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Integration tests for nested attributes.""" |
||
| 280 | def describe_aliases(): |
||
| 281 | |||
| 282 | @pytest.fixture |
||
| 283 | def sample(tmpdir): |
||
| 284 | cls = type('Sample', (), {}) |
||
| 285 | path = str(tmpdir.join("sample.yml")) |
||
| 286 | attrs = dict(var4=NestedList3, var5=StatusDictionary) |
||
| 287 | return yorm.sync(cls(), path, attrs) |
||
| 288 | |||
| 289 | def _log_ref(name, var, ref): |
||
| 290 | logging.info("%s: %r", name, var) |
||
| 291 | logging.info("%s_ref: %r", name, ref) |
||
| 292 | logging.info("%s ID: %s", name, id(var)) |
||
| 293 | logging.info("%s_ref ID: %s", name, id(ref)) |
||
| 294 | assert id(ref) == id(var) |
||
| 295 | assert ref == var |
||
| 296 | |||
| 297 | def test_alias_list(sample): |
||
| 298 | var4_ref = sample.var4 |
||
| 299 | _log_ref('var4', sample.var4, var4_ref) |
||
| 300 | assert [] == sample.var4 |
||
| 301 | |||
| 302 | logging.info("Appending 42 to var4_ref...") |
||
| 303 | var4_ref.append(42) |
||
| 304 | _log_ref('var4', sample.var4, var4_ref) |
||
| 305 | assert [42] == sample.var4 |
||
| 306 | |||
| 307 | logging.info("Appending 2015 to var4_ref...") |
||
| 308 | var4_ref.append(2015) |
||
| 309 | assert [42, 2015] == sample.var4 |
||
| 310 | |||
| 311 | def test_alias_dict(sample): |
||
| 312 | var5_ref = sample.var5 |
||
| 313 | _log_ref('var5', sample.var5, var5_ref) |
||
| 314 | assert {'status': False, 'checked': 0} == sample.var5 |
||
| 315 | |||
| 316 | logging.info("Setting status=True in var5_ref...") |
||
| 317 | var5_ref['status'] = True |
||
| 318 | _log_ref('var5', sample.var5, var5_ref) |
||
| 319 | assert {'status': True, 'checked': 0} == sample.var5 |
||
| 320 | |||
| 321 | logging.info("Setting status=False in var5_ref...") |
||
| 322 | var5_ref['status'] = False |
||
| 323 | _log_ref('var5', sample.var5, var5_ref) |
||
| 324 | assert {'status': False, 'checked': 0} == sample.var5 |
||
| 325 | |||
| 326 | def test_alias_dict_in_list(): |
||
| 327 | top = Top() |
||
| 328 | top.nested_list.append(None) |
||
| 329 | ref1 = top.nested_list[0] |
||
| 330 | ref2 = top.nested_list[0].nested_dict_3 |
||
| 331 | ref3 = top.nested_list[0].nested_dict_3.nested_list_3 |
||
| 332 | assert id(ref1) == id(top.nested_list[0]) |
||
| 333 | assert id(ref2) == id(top.nested_list[0].nested_dict_3) |
||
| 334 | assert id(ref3) == id(top.nested_list[0].nested_dict_3.nested_list_3) |
||
| 335 | |||
| 336 | def test_alias_list_in_dict(): |
||
| 337 | top = Top() |
||
| 338 | logging.info("Updating nested attribute...") |
||
| 339 | top.nested_dict.number = 1 |
||
| 340 | logging.info("Grabbing refs...") |
||
| 341 | ref1 = top.nested_dict |
||
| 342 | ref2 = top.nested_dict.nested_list_2 |
||
| 343 | assert id(ref1) == id(top.nested_dict) |
||
| 344 | assert id(ref2) == id(top.nested_dict.nested_list_2) |
||
| 345 | |||
| 346 | def test_custom_init_is_invoked(sample): |
||
| 347 | sample.__mapper__.text = "var5:\n checked: 42" |
||
| 348 | with expect.raises(RuntimeError): |
||
| 349 | print(sample.var5) |
||
| 350 |