Skip to content

Deprecate old Python 2/3 compatibility layer #4150

Closed

Description

@pekkaklarck

RF 5 supports only Python 3 (#3457) so it doesn't itself anymore need code related to Python 2 support. There are, however, various tools and libraries that use Robot's Python 2/3 compatibility layer and removing it altogether would break all of them. These tools/libs being Python 3 compatible themselves doesn't change that: if they use Robot's API that we remove, they will break.

We could just say that all tools/libs that are compatible with RF 5 should be Python-3-only and shouldn't use the old compatibility layer. Most likely most of them would be updated soon, but there likely are various projects that aren't actively maintained and updating could take time and in the worst case require finding a new maintainer. Breaking lot of projects isn't great, and in this case we can easily avoid that by preserving at least the most important parts of the compatibility layer. I strongly believe that's a good idea, especially because doing that is pretty easy.

Although I think we should preserve the compatibility layer, I don't believe we should preserve it fully. The reason is that there are some utils that exists only to workaround bugs in old Jython and IronPython versions that nobody should be using anymore anyway. Some other utils are so specialized that I don't believe they are used at all. I believe we should preserve the most important parts and then add more utils back during the RF 5 preview release time or even after RF 5.0 release if needed.

This is what I think we should preserve:

  • PY2, PY3, JYTHON, IRONPYTHON and JAVA_VERSION constants
  • py2to3 and py3to2 class decorators
  • is_unicode function (could be an alias for is_string)
  • unicode type (could be an alias for str)
  • unic for safely converting values to Unicode (we still need this utility but it has been renamed to safe_str)
  • roundup function (could be an alias for round even though their semantics aren't exactly the same)

The following utils are likely not used a lot or at all. They can be preserved if needed:

  • with_metaclass for applying a metaclass
  • StringIO (io.StringIO works in most cases also with Python 2)
  • Mapping and MutableMapping (available in collections until Python 3.10)
  • unwrap for unwrapping decorators (available in inspect in Python 3)
  • is_java_init and is_java_method for detecting Java based methods

I believe it's enough to document that the preserved utils are deprecated in RF 5. We can then later decide should we somehow more visible deprecate them. That would be pretty easy with functions but constants are a bit trickier. Most likely there's no actual maintenance or performance burden from preserving these utils so there's no urgent need to remove them either.

Activity

  1. added this to the v5.0 milestone on Nov 24, 2021
  2. pekkaklarck commented on Nov 24, 2021

    @pekkaklarck
    MemberAuthor

    If we want to loudly deprecate this API, it's pretty easy with functions. Just need to do something like this:

    def is_unicode(item):
        warnings.warn("'robot.utils.is_unicode' is deprecated, use 'is_string'. instead.")
        return is_string(item)

    Constants are harder but this ought to work in most cases:

    class DeprecatedConstant:
    
        def __init__(self, name, value):
            self.name = name
            self.value = value
    
        def __eq__(self, other):
            warnings.warn(f"'robot.utils.{self.name}' is deprecated.")
            return self.value == other
    
        def __bool__(self):
            warnings.warn(f"'robot.utils.{self.name}' is deprecated.")
            return bool(self.value)
    
    
    PY2 = DeprecatedConstant('PY2', False)
    PY3 = DeprecatedConstant('PY3', True)
  3. hugovk commented on Nov 25, 2021

    @hugovk
    Contributor
  4. pekkaklarck commented on Nov 27, 2021

    @pekkaklarck
    MemberAuthor

    Thanks for the tip, using module level __getattr__ is a good idea. Python 3.6 is about to reach EOL so Python 3.7+ solution ought to be fine when it's time to more loudly deprecate these.

  5. pekkaklarck commented on Dec 7, 2021

    @pekkaklarck
    MemberAuthor

    SeleniumLibrary uses robot.utils.StringIO (robotframework/SeleniumLibrary#1752). I'm sure they'll remove the usage, but we'd still better add it back to avoid breaking old SL versions.

  6. pekkaklarck commented on Feb 16, 2022

    @pekkaklarck
    MemberAuthor

    robot.utils.platform.PY2 was added back to avoid breaking SSHLibrary. See the issue linked above for details.

  7. pekkaklarck commented on Mar 10, 2022

    @pekkaklarck
    MemberAuthor

    Decided to change all our code to use the standard round instead our Python 2 compatible roundup. Need to preserve roundup still. It's implementation will be just roundup = round which means it won't work exactly the same as earlier. If that's a problem, the old implementation can be preserved.

  8. added a commit that references this issue on Jul 15, 2025
    deb6673
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