You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 10, 2021. It is now read-only.
These errors are some of the most common errors we encounter in Python. They generally have to do with using a value of one type where another type is required.
@@ -494,14 +476,6 @@ In the above example, the `pass` statement is "unnecessary" as the program's eff
494
476
495
477
Good documentation and identifiers are essential for writing software. PyTA helps check to make sure we haven't forgotten to document anything, as well as a basic check on the formatting of our identifiers.
496
478
497
-
### Missing Docstring (C0111) {#C0111}
498
-
499
-
This error occurs when a module, function, class or method has no docstring. Special methods like `__init__` do not require a docstring.
500
-
501
-
~~~~{include="C0111_missing_docstring"}
502
-
~~~~
503
-
504
-
505
479
### Empty Docstring (C0112) {#C0112}
506
480
507
481
This error occurs when a module, function, class or method has an empty docstring.
@@ -646,14 +620,26 @@ This error occurs when we are trying to access a variable from an imported modul
646
620
~~~~
647
621
648
622
649
-
### Import self (W0406) {#W0406}
623
+
### Wildcard import (W0401) {#W0401}
650
624
651
-
A module should not import itself. For example, if we have a module named `W0406_import_self`, it should not import a module with the same name.
625
+
We should only import what we need. Wildcard imports (shown below) are generally discouraged, as they add all objects from the imported module into the global namespace. This makes it difficult to tell in which module a particular class, function or constant is defined, and may cause problems, for example, when multiple modules have objects with identical names.
652
626
653
-
~~~~{include="W0406_import_self"}
627
+
~~~~{include="W0401_wildcard_import"}
654
628
~~~~
655
629
656
-
This error can occur when the name of our Python file conflicts with the name of a module which we would like to import. For example, if we have a Python file named `math.py`, calling `import math` from within that file (or from within *any* Python file in the same directory) will import *our*`math.py` file, and not the [`math` module] from the standard library.
630
+
Rather than importing everything with wildcard `*`, we should specify the names of the objects which we would like to import:
631
+
632
+
```python
633
+
from module_name importSOME_CONSTANT, SomeClass, some_function
634
+
```
635
+
636
+
Or, if we need to import many objects from a particular module, we can import the module itself, and use it as a namespace for the required objects:
637
+
638
+
```python
639
+
import module_name
640
+
641
+
c = module_name.SomeClass()
642
+
```
657
643
658
644
659
645
### Reimported (W0404) {#W0404}
@@ -664,25 +650,34 @@ A module should not be imported more than once.
664
650
~~~~
665
651
666
652
667
-
### Wildcard import (W0401) {#W0401}
653
+
### Import self (W0406) {#W0406}
668
654
669
-
We should only import what we need. Wildcard imports (shown below) are generally discouraged, as they add all objects from the imported module into the global namespace. This makes it difficult to tell in which module a particular class, function or constant is defined, and may cause problems, for example, when multiple modules have objects with identical names.
655
+
A module should not import itself. For example, if we have a module named `W0406_import_self`, it should not import a module with the same name.
670
656
671
-
~~~~{include="W0401_wildcard_import"}
657
+
~~~~{include="W0406_import_self"}
672
658
~~~~
673
659
674
-
Rather than importing everything with wildcard `*`, we should specify the names of the objects which we would like to import:
660
+
This error can occur when the name of our Python file conflicts with the name of a module which we would like to import. For example, if we have a Python file named `math.py`, calling `import math` from within that file (or from within *any* Python file in the same directory) will import *our*`math.py` file, and not the [`math` module] from the standard library.
661
+
662
+
663
+
### Multiple imports (C0410) {#C0410}
664
+
665
+
Different modules should not be imported on a single line.
666
+
667
+
~~~~{include="C0410_multiple_imports"}
668
+
~~~~
669
+
670
+
Rather, each module should be imported on a separate line.
675
671
676
672
```python
677
-
from module_name importSOME_CONSTANT, SomeClass, some_function
673
+
import sys
674
+
import math
678
675
```
679
676
680
-
Or, if we need to import many objects from a particular module, we can import the module itself, and use it as a namespace for the required objects:
677
+
Note, however, that we can import multiple functions, classes, or constants on one line, as long as they are from the same module.
681
678
682
679
```python
683
-
import module_name
684
-
685
-
c = module_name.SomeClass()
680
+
from shutil import copy, SameFileError
686
681
```
687
682
688
683
@@ -694,14 +689,6 @@ This error occurs when the [PEP8 import order][PEP8 Imports] is not respected. W
694
689
~~~~
695
690
696
691
697
-
### Wrong import position (C0413) {#C0413}
698
-
699
-
Imports should be placed at the top of the module, above any other code, but below the module docstring.
700
-
701
-
~~~~{include="C0413_wrong_import_position"}
702
-
~~~~
703
-
704
-
705
692
### Ungrouped imports (C0412) {#C0412}
706
693
707
694
Imports should be grouped by package.
@@ -717,26 +704,13 @@ from math import floor
717
704
```
718
705
719
706
720
-
### Multiple imports (C0410) {#C0410}
707
+
### Wrong import position (C0413) {#C0413}
721
708
722
-
Different modules should not be imported on a single line.
709
+
Imports should be placed at the top of the module, above any other code, but below the module docstring.
723
710
724
-
~~~~{include="C0410_multiple_imports"}
711
+
~~~~{include="C0413_wrong_import_position"}
725
712
~~~~
726
713
727
-
Rather, each module should be imported on a separate line.
728
-
729
-
```python
730
-
import sys
731
-
import math
732
-
```
733
-
734
-
Note, however, that we can import multiple functions, classes, or constants on one line, as long as they are from the same module.
735
-
736
-
```python
737
-
from shutil import copy, SameFileError
738
-
```
739
-
740
714
741
715
### Unused import (W0611) {#W0611}
742
716
@@ -800,7 +774,7 @@ class Composition(object):
800
774
801
775
### Abstract method (W0223) {#W0223}
802
776
803
-
This error occurs when an abstract method (i.e. a method with a `raise NotImplementedError` statement) is not overridden inside a concrete class.
777
+
This error occurs when an abstract method (i.e. a method with a `raise NotImplementedError` statement) is not overridden inside a subclass of the abstract class.
804
778
805
779
~~~~{include="W0223_abstract_method"}
806
780
~~~~
@@ -931,19 +905,19 @@ class SomeNumbers:
931
905
```
932
906
933
907
934
-
### Access to member before definition (E0203) {#E0203}
908
+
### Method hidden (E0202) {#E0202}
935
909
936
-
Before trying to use a member of a class, it should have been defined at some point. If we try to use it before assigning to it, an error will occur.
910
+
If we accidentally hide a method with an attribute, it can cause other code to attempt to invoke what it believes to be a method, which will fail since it has become an attribute instead. This will cause the program to raise an error.
### Access to member before definition (E0203) {#E0203}
943
917
944
-
If we accidentally hide a method with an attribute, it can cause other code to attempt to invoke what it believes to be a method, which will fail since it has become an attribute instead. This will cause the program to raise an error.
918
+
Before trying to use a member of a class, it should have been defined at some point. If we try to use it before assigning to it, an error will occur.
@@ -1062,9 +1038,9 @@ def add_small_coins(nickels: int = 0, dimes: int = 0, quarters: int = 0):
1062
1038
1063
1039
-[W0211](#W0211)
1064
1040
1065
-
### Bad static member (W0211) {#W0211}
1041
+
### Bad static method argument (W0211) {#W0211}
1066
1042
1067
-
This error occurs when a static method contains`self` as the first parameter. Static methods are methods that do not operate on instances. If we feel that the logic of a particular function belongs inside a class, we can move that function into the class and add a [`@staticmethod`][Python Documentation: staticmethod][decorator][Python Documentation: decorator] to signal that the method is a static method which does not take a class instance as the first argument. If such a static method contains `self` as the first parameter, it suggests that we are erroneously expecting a class instance as the first argument to the method.
1043
+
This error occurs when a static method has`self` as the first parameter. Static methods are methods that do not operate on instances. If we feel that the logic of a particular function belongs inside a class, we can move that function into the class and add a [`@staticmethod`][Python Documentation: staticmethod][decorator][Python Documentation: decorator] to signal that the method is a static method which does not take a class instance as the first argument. If such a static method contains `self` as the first parameter, it suggests that we are erroneously expecting a class instance as the first argument to the method.
1068
1044
1069
1045
~~~~{include="W0211_bad_staticmethod_argument"}
1070
1046
~~~~
@@ -1074,20 +1050,21 @@ Corrected version:
1074
1050
```python
1075
1051
classCashRegister:
1076
1052
"""A cash register for storing money and making change."""
This warning occurs when a mutable object, such as a list or dictionary, is provided as a default argument inside a function or a method definition. Default arguments are instantiated only once, at the time when the function or the method is defined (i.e. when the interpreter parses the `def ...` block). If the default argument is modified, it will remain modified in all subsequent calls of the function or method. This leads to a common "gotcha" in Python, where an "empty" list or dictionary, specified as the default argument, starts containing values on calls other than the first call.
1379
+
This warning occurs when a mutable object, such as a list or dictionary, is provided as a default argument in a function definition. Default arguments are instantiated only once, at the time when the function is defined (i.e. when the interpreter encounters the `def ...` block). If the default argument is mutated when the function is called, it will remain modified for all subsequent function calls. This leads to a common "gotcha" in Python, where an "empty" list or dictionary, specified as the default argument, starts containing values on calls other than the first call.
1406
1380
1407
1381
~~~~{include="W0102_dangerous_default_value"}
1408
1382
~~~~
1409
1383
1410
1384
Many new users of Python would expect the output of the code above to be:
This error occurs when the code is indented with a mix of tabs and spaces. Please note that [*spaces are the preferred indentation method*][PEP8: Tabs or Spaces?].
1579
+
This error occurs when an unexpected number of tabs or spaces is used to indent the code. It is recommended that we use [*four spaces per indentation level*][PEP8: Indentation] throughout our code.
1606
1580
1607
-
~~~~{include="W0312_mixed_indentation"}
1581
+
~~~~{include="W0311_bad_indentation"}
1608
1582
~~~~
1609
1583
1610
1584
Corrected version:
1611
1585
1612
1586
```python
1613
-
defhello_world() -> None:
1614
-
"""Greet the universe with a friendly 'Hello World!'."""
1615
-
print("Hello World!")
1587
+
defprint_greeting(name: str) -> None:
1588
+
"""Print a greeting to the person with the given name."""
1589
+
print('Hello {}!'.format(name))
1616
1590
```
1617
1591
1618
1592
1619
-
### Bad indentation (W0311) {#W0311}
1593
+
### Mixed indentation (W0312) {#W0312}
1620
1594
1621
-
This error occurs when an unexpected number of tabs or spaces is used to indent the code. It is recommended that we use [*four spaces per indentation level*][PEP8: Indentation] throughout our code.
1595
+
This error occurs when the code is indented with a mix of tabs and spaces. Please note that [*spaces are the preferred indentation method*][PEP8: Tabs or Spaces?].
1622
1596
1623
-
~~~~{include="W0311_bad_indentation"}
1597
+
~~~~{include="W0312_mixed_indentation"}
1624
1598
~~~~
1625
1599
1626
1600
Corrected version:
1627
1601
1628
1602
```python
1629
-
defprint_greeting(name: str) -> None:
1630
-
"""Print a greeting to the person with the given name."""
1631
-
print('Hello {}!'.format(name))
1603
+
defhello_world() -> None:
1604
+
"""Greet the universe with a friendly 'Hello World!'."""
[StackOverflow: About the changing id of an immutable string]: https://stackoverflow.com/questions/24245324/about-the-changing-id-of-an-immutable-string
1871
1842
[StackOverflow: How To Use The Pass Statement In Python]: https://stackoverflow.com/a/22612774/2063031
1872
1843
[StackOverflow: What does 'super' do in Python?]: https://stackoverflow.com/q/222877/2063031
1873
-
[StackOverflow: What is the difference between \@staticmethod and \@classmethod in Python?]: https://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python
1874
-
[StackOverflow: What's the difference between eval, exec, and compile in Python?]: https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile-in-python
1844
+
[StackOverflow: What is the difference between `@staticmethod` and `@classmethod` in Python?]: https://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python
1875
1845
[StackOverflow: When does Python allocate new memory for identical strings?]: https://stackoverflow.com/questions/2123925/when-does-python-allocate-new-memory-for-identical-strings
0 commit comments