Skip to content

Enhance documentation related to importing same library multiple times #5122

Closed

Description

@oetzus

According to the user guide, "If a library is imported multiple times with different arguments, a new instance is created every time regardless the scope."
But that does not seem to work for me.

Here is the example test case I use:

*** Settings ***
Library          testlib.py    num=1    AS    Test
Library          testlib.py    num=2    AS    Test
Library          testlib.py    num=3    AS    Test

*** Test Cases ***
Example Test
    Log Params
    BuiltIn.Import Library    ${CURDIR}/testlib.py    num=4    AS    Test
    Log Params

with the following example library:

from robot.api import logger

class testlib():

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def __init__(self, num):
        self.num = num

    def log_params(self):
        logger.console(f'num: {self.num}')

As I import the library each time with a different argument, I would expect, that a new instance of the library is created that overwrites the current instance. Instead, the very first instance of the library seems to be used as the output of both calls of Log Params is 1.

Did I understand the note in the user guide wrong or is this an unintended behaviour?

Activity

  1. franzhaas commented on Dec 30, 2024

    @franzhaas
    Contributor

    When you try this you will see that there is a new instance created every time.

    from robot.api import logger
    
    class testlib():
    
        ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    
        def __init__(self, num):
            self.num = num
            logger.error(f"loading {num}")
    
        def log_params(self):
            logger.console(f'num: {self.num}')

    Together with this robot file you can achieve what you are aiming for.:

    *** Settings ***
    Library          testlib.py    num=1      AS  test1
    Library          testlib.py    num=2      AS  test2
    Library          testlib.py    num=3      AS  test3    
    
    *** Test Cases ***
    Example Test
        test3.Log Params
        BuiltIn.Import Library    ${CURDIR}/testlib.py    num=4        AS  test4
        test4.Log Params

    The documentation does not state explicitly which of those loaded library instances will be accessible via the name/be the global instance.

  2. franzhaas commented on Jan 16, 2025

    @franzhaas
    Contributor

    @pekkaklarck can we put this in the 7.3 scope? I think that improving the documentation to make it clear that either it is undefined which of the loaded libraries is going to be used, or that it is going to be the first would be a low risk low effort medium win activity worth doing.

  3. added this to the v7.3 milestone on Feb 11, 2025
  4. pekkaklarck commented on Feb 11, 2025

    @pekkaklarck
    Member

    We can try enhancing docs related to this in RF 7.3. Behavioral changes need to wait for RF 8.0.

  5. franzhaas commented on Feb 19, 2025

    @franzhaas
    Contributor

    We can try enhancing docs related to this in RF 7.3. Behavioral changes need to wait for RF 8.0.

    I have provided a PR for the doc improvement and am happy to react to input and guidance there, but I do not feel comfortable committing to delivering code for a behavior change.

  6. changed the title [-]Multiple imports of library does not create new instance[/-] [+]Enhance documentation related to importing same library multiple times[/+] on Mar 7, 2025
  7. pekkaklarck commented on Apr 6, 2025

    @pekkaklarck
    Member

    I just commented the documentation changes that @franzhaas proposed in PR #5301. Do you @oetzus have opinions about that? Do you see other places where this could be clarified better?

  8. modified the milestones: v7.3, v7.4 on Apr 10, 2025
  9. pekkaklarck commented on Aug 28, 2025

    @pekkaklarck
    Member

    The underlying problem in the original example is that although the library is imported with different arguments, it is always imported with the same alias. That means that the library gets the same name and that causes the latter imported libraries to me ignored. If aliases would be different, for example Test1, Test2, Test3 and Test4, then execution would fail in the first Log Params call like this:

    Multiple keywords with name 'Log Params' found. Give the full name of the keyword you want to use:
    Test1.Log Params
    Test2.Log Params
    Test3.Log Params

    Silently ignoring imported libraries isn't great and should be changed. The same think happens also if you import a library multiple times without an alias, regardless do you use different arguments or not. We have had plans to change the import logic in RF 8.0, but I believe we could emit a warning already in RF 7.4 in these cases. I'll submit a separate issue about that and ask opinion from others on the #devel channel on our Slack. Even if we agree a warning is a good idea, it probably makes sense to check what the User Guide says about this.

  10. self-assigned this
    on Sep 2, 2025
  11. pekkaklarck commented on Sep 2, 2025

    @pekkaklarck
    Member

    After #5492 being implemented, running the original example gives you three warnings like this:

    [ WARN ] Error in file '/path/to/example.robot' on line 3: Suite 'Example' has already imported library 'Test' with different arguments. This import is ignored.

    These warnings ought to make it clear that subsequent imports are ignored, so there are less needs to document this behavior in detailed level.

    The above said, I noticed that the User Guide section about providing arguments to libraries (it's the section before the section about library scopes linked from the description, and the library scope section has a link to it as well) didn't mention the need to use custom names if same library is imported multiple times. I'll enhance that section in this regard. I believe that, in combination with the new warning, is enough to consider this issue done.

  12. added a commit that references this issue on Sep 2, 2025
    ce7f995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions