2023-07-03 09:02:23 +02:00
This rule raises an issue when a name listed in the ``++__all__++`` property of a module has not been defined.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-03 09:02:23 +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.
In the following example:
[source,python]
----
# mymodule.py
2021-04-28 16:49:39 +02:00
2023-07-03 09:02:23 +02:00
def foo(): ...
def bar(): ...
2021-04-28 18:08:03 +02:00
2023-07-03 09:02:23 +02:00
__all__ = ["foo"]
----
Executing ``++from mymodule import *++`` from a different module will only import `foo`.
This list can only reference defined names, otherwise an ``++AttributeError++`` will be raised when the module is imported.
=== Code examples
==== Noncompliant code 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
----
from mymodule import my_func
2023-07-03 09:02:23 +02:00
__all__ = ["unknown_func"] # Noncompliant: "unknown_func" is undefined
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-07-03 09:02:23 +02:00
==== Compliant solution
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
----
from mymodule import my_func
__all__ = ["my_func"]
----
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 09:02:23 +02:00
=== Documentation
* 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
Change or remove this string; "XX" is not defined.
=== Highlighting
* Primary: The string with an undefined name.
* Secondary: the variable assignment if a variable is used.
message: 'Assigned here.'
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
=== relates to: S2823
=== relates to: S3827
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]