New library instance at each Import Library

I’m using a library to interface with other processes (an extended pexpect).
I have a
Import Library | Process_Mock | ${COMMAND_PATH} | WITH NAME | ABC
at the start of some test cases.

The second call to Import Library does not instantiate a new object, so only the first test case is executed correctly.

I would like to instantiate a fresh object of the underlying class (with the same name), how can I achieve that?

Robot Framework has some fuzzy logic when library import causes instance creation. But if you want to create new instance for each test or suite, then the correct solution is explained in here: Robot Framework User Guide

DUH - I forgot about library scopes…

Unfortunately changing from GLOBAL to TEST did not work for me (RF version 4.1.3 on Python 3.6.8).
Perhaps that can have something to do with me instantiating two different objects of the same Process_Mock class in the same test cases?

Using GLOBAL each (of the three) test case works fine, when run individually, and only the first succeeds if all is run. Using TEST only the first works, even when run individually (!?!)

The library looks like:

import pexpect
import os
from robot.api.deco import library
from robot.libraries.BuiltIn import BuiltIn

#------------------------------------------------------------------------#                                

@library(scope='TEST', version='0.1', auto_keywords=True)
class Process_Mock (pexpect.spawn):
    def __init__ (self, Name: str, Command: str, *ignored):
        env = dict(os.environ);
        env["PROCESS_NAME"] = Name;
        super().__init__ (Command, env=env, timeout=1.0);

Furthermore, I have a test case where I want to test that the component-under-test correctly handles abrupt socket disconnects, so I was considering doing that by freeing the (named) Process_Mock instance and creating a new one. I’m not quite sure how to do that in a good way…

Here is a MWE demonstrating my issue:

my_lib.py:

from robot.api.deco import library

#@library(scope='TEST', version='0.1', auto_keywords=True)
class my_lib:
    ROBOT_LIBRARY_SCOPE = 'TEST'
    def __init__ (self, Name: str, *ignored):
        self.f_name = Name

    def Name (self):
	    return self.f_name

my_test.robot:

*** Test Cases ***

Test Library Instantiation
| | Import Library | my_lib | Lib1 | WITH NAME | A
| | Import Library | my_lib | Lib2 | WITH NAME | B
| | Names should be | Lib1 | Lib2
| |
| | Import Library | my_lib | New_Lib2 | WITH NAME | B
| | Names should be | Lib1 | New_Lib2

Test Second Library Instantiation
| | Import Library | my_lib | Lib1b | WITH NAME | A
| | Import Library | my_lib | Lib2b | WITH NAME | B
| | Names should be | Lib1b | Lib2b


*** Keywords ***
Names should be
| | [Arguments] | ${NAMEA} | ${NAMEB}
| | ${actual_A}= | A.Name
| | ${actual_B}= | B.Name
| | Should be equal | ${NAMEA} | ${actual_A}
| | Should be equal | ${NAMEB} | ${actual_B}

On my installation I get:

$ robot -P . my_test.robot
==============================================================================
My Test                                                                       
==============================================================================
Test Library Instantiation                                            | FAIL |
New_Lib2 != Lib2
------------------------------------------------------------------------------
Test Second Library Instantiation                                     | FAIL |
Lib1b != Lib1
------------------------------------------------------------------------------
My Test                                                               | FAIL |
2 tests, 0 passed, 2 failed

Neither of the results seem to be right…