83 lines
1.6 KiB
Plaintext
83 lines
1.6 KiB
Plaintext
This rule raises an issue when the ``++__all__++`` property of a module contains objects that aren't strings.
|
|
|
|
== Why is this an issue?
|
|
|
|
The ``++__all__++`` property of a module is used to define the list of names that will be imported when performing a wildcard import of this module, i.e. when ``++from mymodule import *++`` is used.
|
|
|
|
In the following example:
|
|
|
|
[source,python]
|
|
----
|
|
# mymodule.py
|
|
def foo(): ...
|
|
def bar(): ...
|
|
__all__ = ["foo"]
|
|
----
|
|
|
|
Executing ``++from mymodule import *++`` from a different module will only import `foo`.
|
|
|
|
This list can only contain strings. If something other than a string is listed, a `TypeError` will be raised when trying to perform a wildcard import of the module.
|
|
|
|
|
|
To fix this issue, make sure that all properties listed in ``++__all__++`` are strings.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python, diff-id=1,diff-type=noncompliant]
|
|
----
|
|
class MyClass:
|
|
pass
|
|
|
|
__all__ = [
|
|
MyClass # Noncompliant: wildcard import will raise a TypeError
|
|
]
|
|
----
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
----
|
|
class MyClass:
|
|
pass
|
|
|
|
__all__ = [
|
|
"MyClass"
|
|
]
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://docs.python.org/3/tutorial/modules.html#importing-from-a-package[Python documentation - Importing * From a Package]
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Replace this symbol with a string; "__all__" can only contain strings.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Primary: the wrong symbol in "__all__"
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== is related to: S5807
|
|
|
|
endif::env-github,rspecator-view[]
|