Skip to content

Automatically import modules that are used with Evaluate, Run Keyword If, and others #3349

Closed

Description

@pekkaklarck

Currently BuiltIn keywords like Run Keyword If and Should Be True that evaluate Python expressions automatically add sys and os modules to the evaluation namespace (#1449) and Evaluate has a separate modules argument to specify which modules to add to the namespace (#240). We are now adding inline Python evaluation support (#3179) and need to decide how to handle modules with it. It would benefit from more modules than sys and os, but deciding what modules to add automatically isn't easy and it's not easy to come up with a good syntax to explicitly add modules either. The best idea discussed there seems to be automatically importing modules that are used.

Automatically importing modules basically means that if an expression like foo.bar is evaluated, and foo isn't found from the evaluation namespace otherwise, we'd automatically try to import module foo. Without this functionality foo.bar would resort to an error like NameError: name 'foo' is not defined, so the change ought to be fully backwards compatible.

One thing to think about is how to handle failed module imports. If we just let the original exception from Python to go through, the error will be something like ImportError: No module named foo. This is OK if there was an idea to import a module, but if there's a typo like lem(...) (not len(...)) then it can be pretty strange. Perhaps we should provide a custom message telling something like 'lem' was not found nor importable as a module.

If this functionality is implemented, the current modules argument with Evaluate won't have any effect anymore. We can deprecate it but because there's no hurry removing, we shouldn't do it "loudly" in the beginning. Instead we can document it's not anymore needed with RF 3.2 and ignore it. Deprecating it so that there's a warning can wait until RF 3.3 or later.

Activity

  1. added this to the v3.2 milestone on Oct 20, 2019
  2. pekkaklarck commented on Oct 21, 2019

    @pekkaklarck
    MemberAuthor

    In comments of #3179 @stdedos asked about this usage that has been commonly used with SeleniumLibrary to open browser with custom configuration:

        ${chrome options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()
        ...                     modules=sys, selenium.webdriver
    

    My initial reaction was to use

        ${chrome options} =     Evaluate    selenium.webdriver.ChromeOptions()
        ...                     modules=selenium
    

    instead (and in the future omit modules altogether), but it turned this usage fails with AttributeError: module 'selenium' has no attribute 'webdriver'. This is due to how the selenium module has been implemented. You can try also on the interpreter that only importing selenium doesn't make selenium.webdriver submodule available, you need to explicitly import it:

    >>> import selenium
    >>> selenium.webdriver
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'webdriver'
    >>> import selenium.webdriver
    >>> selenium.webdriver
    <module 'selenium.webdriver' from '/path/selenium/webdriver/__init__.pyc'>

    Then I thought that at least

        ${chrome options} =     Evaluate    selenium.webdriver.ChromeOptions()
        ...                     modules=selenium.webdriver
    

    would work, but unfortunately that fails with NameError: name 'selenium' is not defined. Apparently now only the selenium.webdriver submodule is in the evaluation namespace and selenium alone isn't found. This is pretty strange, even the interpreter works differently, but we cannot really fight against Python's import/evaluation machinery. The original example avoids this problem by telling Evaluate to import selenium.webdriver and then finding it from sys.modules by name. That's clever but in my opinion this solution (that also works) is a bit cleaner:

        ${chrome options} =     Evaluate    selenium.webdriver.ChromeOptions()
        ...                     modules=selenium, selenium.webdriver
    

    It's a bit annoying (and definitely not obvious) that we need to tell Evaluate to import both selenium and selenium.webdriver modules. Still better than using sys.modules in my opinion.

    What does all that mean? Well, many things:

    1. The approach to automatically import module foo when used like foo.bar.zap doesn't necessarily work if bar is a submodule. This depends on the module implementation. (If we really wanted, we could solve this by returning a proxy object that wraps foo and automatically imports submodules when accessed like foo.bar, but I seriously doubt that's worth the effort.)

    2. The above means modules with Evaluate are still needed. They just aren't needed as often as they used to be.

    3. We could consider handling modules=root.submodule a bit better to avoid the need to use modules=root, root.submodule. We could simply import both root and root.submodule in the former case as well. That would make the example above with modules=selenium.webdriver work. This enhancement requires a separate issue.

    Finally, Open Browser in SeleniumLibrary 4.0 and newer has better support for custom browser configuration making the need to construct ChromeOptions like this pretty rare. It may still be needed sometimes and similar needs may arise with other libraries, so the underlying problem with Evaluate is still relevant.

  3. stdedos commented on Oct 21, 2019

    @stdedos
    Contributor

    In comments of #3179 @stdedos asked about this usage that has been commonly used with SeleniumLibrary to open browser with custom configuration:

        ${chrome options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()
        ...                     modules=sys, selenium.webdriver

    Note that ^^ this is coming from e.g.
    https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Create%20Webdriver

    I will not attempt to fix it, as I cannot clain to fully understand Selenium

    Maybe this vv should be added in its place (if also applicable)

    That's clever but in my opinion this solution (that also works) is a bit cleaner:

        ${chrome options} =     Evaluate    selenium.webdriver.ChromeOptions()
        ...                     modules=selenium, selenium.webdriver
  4. pekkaklarck commented on Oct 21, 2019

    @pekkaklarck
    MemberAuthor

    That's a strange example in SL docs! Ping @aaltat.

    Anyway, it would still continue to work

  5. aaltat commented on Oct 21, 2019

    @aaltat
    Contributor
  6. pekkaklarck commented on Oct 29, 2019

    @pekkaklarck
    MemberAuthor

    Submitted #3355 about automatically importing parent modules when using modules=rootmod.submod.

    Implementation of this issue as well as highly related #3179 and #3355 is ready and I'll commit changes shortly. Documentation still missing but I hope to get that done soon as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions