コード例 #1
0
def test_stubs_manager(tmp_path: Path):
    stubs = StubsManager()
    root = tmp_path / 'project'
    root.mkdir()
    path = root / 'example.py'

    # test create
    stubs.create(path)
    assert set(stubs._modules) == {'example'}
    assert stubs._modules['example']._content == {}

    # test get
    assert stubs.get('example') is stubs._modules['example']
    expected = {'raises': ['AssertionError', 'TypeError']}
    assert stubs.get('typing')._content['get_type_hints'] == expected

    # test do not re-create already cached stub
    old_stub = stubs.get('example')
    old_stub.dump()
    new_stub = stubs.create(path)
    assert new_stub is old_stub
    assert stubs.get('example') is old_stub

    # the same but path leads to stub, not code
    new_stub = stubs.create(path.with_suffix('.json'))
    assert new_stub is old_stub
    assert stubs.get('example') is old_stub

    # read already dumped stub instead of creating
    old_stub.add(func='fname', contract=Category.RAISES, value='TypeError')
    old_stub.dump()
    stubs = StubsManager()
    new_stub = stubs.create(path)
    assert new_stub is not old_stub
    assert new_stub._content == {'fname': {'raises': ['TypeError']}}

    # test read with non-stub extensions
    stub = stubs.read(path=path)
    assert stub.path.name == 'example.json'
    with pytest.raises(ValueError, match='invalid stub file extension.*'):
        stubs.read(path=path.with_suffix('.cpp'))
コード例 #2
0
def test_marshmallow_get_stubs():
    stubs = StubsManager()
    stub = stubs.get('marshmallow.utils')
    assert stub is not None