diff --git a/mypy/parse.py b/mypy/parse.py index f440be5d3d85..cc1b9153f5dd 100644 --- a/mypy/parse.py +++ b/mypy/parse.py @@ -32,12 +32,14 @@ def parse( parse contents from path `fnam` if `source` is `None`. """ if options.native_parser: - import mypy.nativeparse + # Use import from as workaround for cpython#149728 - accessing mypy.nativeparse + # after importing it may fail when this function is called concurrently. + from mypy import nativeparse ignore_errors = options.ignore_errors or fnam in errors.ignored_files # If errors are ignored, we can drop many function bodies to speed up type checking. strip_function_bodies = ignore_errors and not options.preserve_asts - tree, _, _ = mypy.nativeparse.native_parse( + tree, _, _ = nativeparse.native_parse( fnam, options, source, skip_function_bodies=strip_function_bodies ) # Set is_stub based on file extension @@ -120,9 +122,11 @@ def report_parse_error(error: ParseError, errors: Errors) -> None: def parse_type_string(expr: StrExpr, options: Options) -> ProperType: if options.native_parser: - import mypy.nativeparse + # Use a import from as workaround for cpython#149728 - accessing mypy.nativeparse + # after importing it may fail when this function is called concurrently. + from mypy import nativeparse - return mypy.nativeparse.native_parse_type_string( + return nativeparse.native_parse_type_string( expr.value, expr.line, expr.column, diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py index b558127d31dc..ced443e94f3d 100644 --- a/mypy/test/testcmdline.py +++ b/mypy/test/testcmdline.py @@ -13,7 +13,8 @@ import sys import sysconfig -from mypy.test.config import PREFIX, test_temp_dir +import mypy +from mypy.test.config import test_temp_dir from mypy.test.data import DataDrivenTestCase, DataSuite from mypy.test.helpers import ( assert_string_arrays_equal, @@ -72,7 +73,8 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None: env = os.environ.copy() env.pop("COLUMNS", None) extra_path = os.path.join(os.path.abspath(test_temp_dir), "pypath") - env["PYTHONPATH"] = PREFIX + # Use the same mypy installation as pytest, including the compiled wheel in CI. + env["PYTHONPATH"] = os.path.dirname(os.path.abspath(mypy.__path__[0])) if os.path.isdir(extra_path): env["PYTHONPATH"] += os.pathsep + extra_path cwd = os.path.join(test_temp_dir, custom_cwd or "")