2023-07-03 11:35:19 +02:00
This rule raises an issue when the ``++__all__++`` property of a module contains objects that aren't strings.
2023-05-03 11:06:20 +02:00
2023-07-03 11:35:19 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
2023-07-03 11:35:19 +02:00
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.
2021-04-28 18:08:03 +02:00
2023-07-03 11:35:19 +02:00
In the following example:
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
2023-07-03 11:35:19 +02:00
# 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]
----
2021-04-28 16:49:39 +02:00
class MyClass:
pass
__all__ = [
2023-07-03 11:35:19 +02:00
MyClass # Noncompliant: wildcard import will raise a TypeError
2021-04-28 16:49:39 +02:00
]
----
2021-04-28 18:08:03 +02:00
2023-07-03 11:35:19 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-07-03 11:35:19 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
class MyClass:
pass
__all__ = [
"MyClass"
]
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-07-03 11:35:19 +02:00
=== Documentation
2021-04-28 16:49:39 +02:00
* https://docs.python.org/3/tutorial/modules.html#importing-from-a-package[Python documentation - Importing * From a Package]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace this symbol with a string; "__all__" can only contain strings.
=== Highlighting
Primary: the wrong symbol in "__all__"
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is related to: S5807
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]