Skip to content

KeyError in res_strict.graph in Translator when using external library (numpy) #159

@ifndefJOSH

Description

@ifndefJOSH

This may be an issue with mypy and not Nagini, but I discovered an issue when attempting to verify the following minimal Numpy program:

 import numpy as np

from nagini_contracts.contracts import *

def matmult(A : np.matrix, B : np.matrix) -> np.matrix:
	Requires(A.size == B.size)
	Ensures(Result().size == A.size)
	return A * B

The following KeyError is produced in Nagini

Traceback (most recent call last):
  File "/home/josh/.pyenv/versions/3.8.18/bin/nagini", line 8, in <module>
    sys.exit(main())
  File "/home/josh/.pyenv/versions/3.8.18/lib/python3.8/site-packages/nagini_translation/main.py", line 352, in main
    translate_and_verify(args.python_file, jvm, args, arp=args.arp, base_dir=args.base_dir)
  File "/home/josh/.pyenv/versions/3.8.18/lib/python3.8/site-packages/nagini_translation/main.py", line 359, in translate_and_verify
    modules, prog = translate(python_file, jvm, selected=selected, sif=args.sif, base_dir=base_dir,
  File "/home/josh/.pyenv/versions/3.8.18/lib/python3.8/site-packages/nagini_translation/main.py", line 110, in translate
    type_correct = types.check(path, base_dir)
  File "/home/josh/.pyenv/versions/3.8.18/lib/python3.8/site-packages/nagini_translation/lib/typeinfo.py", line 339, in check
    for dep_name in res_strict.graph[name].dep_line_map:
KeyError: 'numpy.core._multiarray_umath'

Notes

  • I was unable to reproduce this with other libraries, such as pillow (because of insufficient type annotations)
  • This happens on any numpy program I attempt to verify
  • numpy.core._multiarray_umath is not a type but rather a module within numpy.
  • I've tried this with several versions of numpy, with no difference

My thoughts (as an outsider to your project)

Digging around in your source code, it is evident that res_strict is built by mypy, and name (the variable causing the KeyError) is produced using each of all elements in relevant_files which is directly defined from [next(iter(res_strict.graph))]. This causes the KeyError to be especially confusing, as these are all supposed to be keys from res_strict.graph. Spitballing, it could be related to #37 but honestly it seems like an issue with mypy's Dict (since that's the type of res_strict.graph as you can see here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions