From 7c242060fcd8fa5e3a1e72f9d6a6772376580d0e Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Mon, 21 Oct 2019 17:26:57 +0300 Subject: [PATCH] Enable pyi-stubs for `__future__`, `cPickle` and `numbers` No `Lib` and `python_stubs` folders in MockSDK2.* No `python_stubs` folder in MockSDK3.* GitOrigin-RevId: 9918e003b86ba1fde0c85ab56469c586f0f07171 --- python/helpers/typeshed/stdlib/2/cPickle.pyi | 32 ++ .../typeshed/stdlib/2and3/__future__.pyi | 24 ++ .../helpers/typeshed/stdlib/2and3/numbers.pyi | 144 +++++++ .../jetbrains/python/fixtures/PyTestCase.java | 60 ++- python/testData/MockSdk2.7/Lib/__future__.py | 128 ------ python/testData/MockSdk2.7/Lib/numbers.py | 391 ------------------ .../MockSdk2.7/python_stubs/cPickle.py | 126 ------ python/testData/MockSdk3.7/Lib/numbers.py | 389 ----------------- .../com/jetbrains/python/PyIndexingTest.java | 57 +-- .../jetbrains/python/tools/PyTypeShedSync.kts | 3 + 10 files changed, 286 insertions(+), 1068 deletions(-) create mode 100644 python/helpers/typeshed/stdlib/2/cPickle.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/__future__.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/numbers.pyi delete mode 100644 python/testData/MockSdk2.7/Lib/__future__.py delete mode 100644 python/testData/MockSdk2.7/Lib/numbers.py delete mode 100644 python/testData/MockSdk2.7/python_stubs/cPickle.py delete mode 100644 python/testData/MockSdk3.7/Lib/numbers.py diff --git a/python/helpers/typeshed/stdlib/2/cPickle.pyi b/python/helpers/typeshed/stdlib/2/cPickle.pyi new file mode 100644 index 000000000000..0421c50efe17 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2/cPickle.pyi @@ -0,0 +1,32 @@ +from typing import Any, IO, List + +HIGHEST_PROTOCOL: int +compatible_formats: List[str] +format_version: str + +class Pickler: + def __init__(self, file: IO[str], protocol: int = ...) -> None: ... + + def dump(self, obj: Any) -> None: ... + + def clear_memo(self) -> None: ... + + +class Unpickler: + def __init__(self, file: IO[str]) -> None: ... + + def load(self) -> Any: ... + + def noload(self) -> Any: ... + + +def dump(obj: Any, file: IO[str], protocol: int = ...) -> None: ... +def dumps(obj: Any, protocol: int = ...) -> str: ... +def load(file: IO[str]) -> Any: ... +def loads(str: str) -> Any: ... + +class PickleError(Exception): ... +class UnpicklingError(PickleError): ... +class BadPickleGet(UnpicklingError): ... +class PicklingError(PickleError): ... +class UnpickleableError(PicklingError): ... diff --git a/python/helpers/typeshed/stdlib/2and3/__future__.pyi b/python/helpers/typeshed/stdlib/2and3/__future__.pyi new file mode 100644 index 000000000000..13db2dc6b899 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/__future__.pyi @@ -0,0 +1,24 @@ +import sys +from typing import List + +class _Feature: + def getOptionalRelease(self) -> sys._version_info: ... + def getMandatoryRelease(self) -> sys._version_info: ... + +absolute_import: _Feature +division: _Feature +generators: _Feature +nested_scopes: _Feature +print_function: _Feature +unicode_literals: _Feature +with_statement: _Feature +if sys.version_info >= (3, 0): + barry_as_FLUFL: _Feature + +if sys.version_info >= (3, 5): + generator_stop: _Feature + +if sys.version_info >= (3, 7): + annotations: _Feature + +all_feature_names: List[str] diff --git a/python/helpers/typeshed/stdlib/2and3/numbers.pyi b/python/helpers/typeshed/stdlib/2and3/numbers.pyi new file mode 100644 index 000000000000..befe7d53a781 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/numbers.pyi @@ -0,0 +1,144 @@ +# Stubs for numbers (Python 3.5) +# See https://docs.python.org/2.7/library/numbers.html +# and https://docs.python.org/3/library/numbers.html +# +# Note: these stubs are incomplete. The more complex type +# signatures are currently omitted. + +from typing import Any, Optional, SupportsFloat, overload +from abc import ABCMeta, abstractmethod +import sys + +class Number(metaclass=ABCMeta): + @abstractmethod + def __hash__(self) -> int: ... + +class Complex(Number): + @abstractmethod + def __complex__(self) -> complex: ... + if sys.version_info >= (3, 0): + def __bool__(self) -> bool: ... + else: + def __nonzero__(self) -> bool: ... + @property + @abstractmethod + def real(self): ... + @property + @abstractmethod + def imag(self): ... + @abstractmethod + def __add__(self, other): ... + @abstractmethod + def __radd__(self, other): ... + @abstractmethod + def __neg__(self): ... + @abstractmethod + def __pos__(self): ... + def __sub__(self, other): ... + def __rsub__(self, other): ... + @abstractmethod + def __mul__(self, other): ... + @abstractmethod + def __rmul__(self, other): ... + if sys.version_info < (3, 0): + @abstractmethod + def __div__(self, other): ... + @abstractmethod + def __rdiv__(self, other): ... + @abstractmethod + def __truediv__(self, other): ... + @abstractmethod + def __rtruediv__(self, other): ... + @abstractmethod + def __pow__(self, exponent): ... + @abstractmethod + def __rpow__(self, base): ... + def __abs__(self): ... + def conjugate(self): ... + def __eq__(self, other: object) -> bool: ... + if sys.version_info < (3, 0): + def __ne__(self, other: object) -> bool: ... + +class Real(Complex, SupportsFloat): + @abstractmethod + def __float__(self) -> float: ... + @abstractmethod + def __trunc__(self) -> int: ... + if sys.version_info >= (3, 0): + @abstractmethod + def __floor__(self) -> int: ... + @abstractmethod + def __ceil__(self) -> int: ... + @abstractmethod + @overload + def __round__(self, ndigits: None = ...): ... + @abstractmethod + @overload + def __round__(self, ndigits: int): ... + def __divmod__(self, other): ... + def __rdivmod__(self, other): ... + @abstractmethod + def __floordiv__(self, other): ... + @abstractmethod + def __rfloordiv__(self, other): ... + @abstractmethod + def __mod__(self, other): ... + @abstractmethod + def __rmod__(self, other): ... + @abstractmethod + def __lt__(self, other) -> bool: ... + @abstractmethod + def __le__(self, other) -> bool: ... + def __complex__(self) -> complex: ... + @property + def real(self): ... + @property + def imag(self): ... + def conjugate(self): ... + +class Rational(Real): + @property + @abstractmethod + def numerator(self) -> int: ... + @property + @abstractmethod + def denominator(self) -> int: ... + def __float__(self) -> float: ... + +class Integral(Rational): + if sys.version_info >= (3, 0): + @abstractmethod + def __int__(self) -> int: ... + else: + @abstractmethod + def __long__(self) -> long: ... + def __index__(self) -> int: ... + @abstractmethod + def __pow__(self, exponent, modulus: Optional[Any] = ...): ... + @abstractmethod + def __lshift__(self, other): ... + @abstractmethod + def __rlshift__(self, other): ... + @abstractmethod + def __rshift__(self, other): ... + @abstractmethod + def __rrshift__(self, other): ... + @abstractmethod + def __and__(self, other): ... + @abstractmethod + def __rand__(self, other): ... + @abstractmethod + def __xor__(self, other): ... + @abstractmethod + def __rxor__(self, other): ... + @abstractmethod + def __or__(self, other): ... + @abstractmethod + def __ror__(self, other): ... + @abstractmethod + def __invert__(self): ... + def __float__(self) -> float: ... + @property + def numerator(self) -> int: ... + @property + def denominator(self) -> int: ... diff --git a/python/python-psi-impl/test/com/jetbrains/python/fixtures/PyTestCase.java b/python/python-psi-impl/test/com/jetbrains/python/fixtures/PyTestCase.java index b0973e1e8e1c..37a065a5e9a0 100644 --- a/python/python-psi-impl/test/com/jetbrains/python/fixtures/PyTestCase.java +++ b/python/python-psi-impl/test/com/jetbrains/python/fixtures/PyTestCase.java @@ -165,25 +165,47 @@ public abstract class PyTestCase extends UsefulTestCase { protected void runWithAdditionalFileInLibDir(@NotNull String relativePath, @NotNull String text, - @NotNull Consumer consumer) { + @NotNull Consumer fileConsumer) { final Sdk sdk = PythonSdkUtil.findPythonSdk(myFixture.getModule()); - runWithAdditionalFileIn(relativePath, text, PySearchUtilBase.findLibDir(sdk), consumer); + final VirtualFile libDir = PySearchUtilBase.findLibDir(sdk); + if (libDir != null) { + runWithAdditionalFileIn(relativePath, text, libDir, fileConsumer); + } + else { + createAdditionalRootAndRunWithIt( + sdk, + "Lib", + OrderRootType.CLASSES, + root -> runWithAdditionalFileIn(relativePath, text, root, fileConsumer) + ); + } } protected void runWithAdditionalFileInSkeletonDir(@NotNull String relativePath, @NotNull String text, - @NotNull Consumer consumer) { + @NotNull Consumer fileConsumer) { final Sdk sdk = PythonSdkUtil.findPythonSdk(myFixture.getModule()); - runWithAdditionalFileIn(relativePath, text, PythonSdkUtil.findSkeletonsDir(sdk), consumer); + final VirtualFile skeletonsDir = PythonSdkUtil.findSkeletonsDir(sdk); + if (skeletonsDir != null) { + runWithAdditionalFileIn(relativePath, text, skeletonsDir, fileConsumer); + } + else { + createAdditionalRootAndRunWithIt( + sdk, + PythonSdkUtil.SKELETON_DIR_NAME, + PythonSdkUtil.BUILTIN_ROOT_TYPE, + root -> runWithAdditionalFileIn(relativePath, text, root, fileConsumer) + ); + } } private static void runWithAdditionalFileIn(@NotNull String relativePath, @NotNull String text, @NotNull VirtualFile dir, - @NotNull Consumer consumer) { + @NotNull Consumer fileConsumer) { final VirtualFile file = VfsTestUtil.createFile(dir, relativePath, text); try { - consumer.accept(file); + fileConsumer.accept(file); } finally { VfsTestUtil.deleteFile(file); @@ -193,20 +215,40 @@ public abstract class PyTestCase extends UsefulTestCase { protected void runWithAdditionalClassEntryInSdkRoots(@NotNull VirtualFile directory, @NotNull Runnable runnable) { final Sdk sdk = PythonSdkUtil.findPythonSdk(myFixture.getModule()); assertNotNull(sdk); + runWithAdditionalRoot(sdk, directory, OrderRootType.CLASSES, (__) -> runnable.run()); + } + + private static void createAdditionalRootAndRunWithIt(@NotNull Sdk sdk, + @NotNull String rootRelativePath, + @NotNull OrderRootType rootType, + @NotNull Consumer rootConsumer) { + final VirtualFile tempRoot = VfsTestUtil.createDir(sdk.getHomeDirectory().getParent().getParent(), rootRelativePath); + try { + runWithAdditionalRoot(sdk, tempRoot, rootType, rootConsumer); + } + finally { + VfsTestUtil.deleteFile(tempRoot); + } + } + + private static void runWithAdditionalRoot(@NotNull Sdk sdk, + @NotNull VirtualFile root, + @NotNull OrderRootType rootType, + @NotNull Consumer rootConsumer) { WriteAction.run(() -> { final SdkModificator modificator = sdk.getSdkModificator(); assertNotNull(modificator); - modificator.addRoot(directory, OrderRootType.CLASSES); + modificator.addRoot(root, rootType); modificator.commitChanges(); }); try { - runnable.run(); + rootConsumer.accept(root); } finally { WriteAction.run(() -> { final SdkModificator modificator = sdk.getSdkModificator(); assertNotNull(modificator); - modificator.removeRoot(directory, OrderRootType.CLASSES); + modificator.removeRoot(root, rootType); modificator.commitChanges(); }); } diff --git a/python/testData/MockSdk2.7/Lib/__future__.py b/python/testData/MockSdk2.7/Lib/__future__.py deleted file mode 100644 index 915645933c03..000000000000 --- a/python/testData/MockSdk2.7/Lib/__future__.py +++ /dev/null @@ -1,128 +0,0 @@ -"""Record of phased-in incompatible language changes. - -Each line is of the form: - - FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease "," - CompilerFlag ")" - -where, normally, OptionalRelease < MandatoryRelease, and both are 5-tuples -of the same form as sys.version_info: - - (PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int - PY_MINOR_VERSION, # the 1; an int - PY_MICRO_VERSION, # the 0; an int - PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string - PY_RELEASE_SERIAL # the 3; an int - ) - -OptionalRelease records the first release in which - - from __future__ import FeatureName - -was accepted. - -In the case of MandatoryReleases that have not yet occurred, -MandatoryRelease predicts the release in which the feature will become part -of the language. - -Else MandatoryRelease records when the feature became part of the language; -in releases at or after that, modules no longer need - - from __future__ import FeatureName - -to use the feature in question, but may continue to use such imports. - -MandatoryRelease may also be None, meaning that a planned feature got -dropped. - -Instances of class _Feature have two corresponding methods, -.getOptionalRelease() and .getMandatoryRelease(). - -CompilerFlag is the (bitfield) flag that should be passed in the fourth -argument to the builtin function compile() to enable the feature in -dynamically compiled code. This flag is stored in the .compiler_flag -attribute on _Future instances. These values must match the appropriate -#defines of CO_xxx flags in Include/compile.h. - -No feature line is ever to be deleted from this file. -""" - -all_feature_names = [ - "nested_scopes", - "generators", - "division", - "absolute_import", - "with_statement", - "print_function", - "unicode_literals", -] - -__all__ = ["all_feature_names"] + all_feature_names - -# The CO_xxx symbols are defined here under the same names used by -# compile.h, so that an editor search will find them here. However, -# they're not exported in __all__, because they don't really belong to -# this module. -CO_NESTED = 0x0010 # nested_scopes -CO_GENERATOR_ALLOWED = 0 # generators (obsolete, was 0x1000) -CO_FUTURE_DIVISION = 0x2000 # division -CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default -CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement -CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function -CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals - -class _Feature: - def __init__(self, optionalRelease, mandatoryRelease, compiler_flag): - self.optional = optionalRelease - self.mandatory = mandatoryRelease - self.compiler_flag = compiler_flag - - def getOptionalRelease(self): - """Return first release in which this feature was recognized. - - This is a 5-tuple, of the same form as sys.version_info. - """ - - return self.optional - - def getMandatoryRelease(self): - """Return release in which this feature will become mandatory. - - This is a 5-tuple, of the same form as sys.version_info, or, if - the feature was dropped, is None. - """ - - return self.mandatory - - def __repr__(self): - return "_Feature" + repr((self.optional, - self.mandatory, - self.compiler_flag)) - -nested_scopes = _Feature((2, 1, 0, "beta", 1), - (2, 2, 0, "alpha", 0), - CO_NESTED) - -generators = _Feature((2, 2, 0, "alpha", 1), - (2, 3, 0, "final", 0), - CO_GENERATOR_ALLOWED) - -division = _Feature((2, 2, 0, "alpha", 2), - (3, 0, 0, "alpha", 0), - CO_FUTURE_DIVISION) - -absolute_import = _Feature((2, 5, 0, "alpha", 1), - (2, 7, 0, "alpha", 0), - CO_FUTURE_ABSOLUTE_IMPORT) - -with_statement = _Feature((2, 5, 0, "alpha", 1), - (2, 6, 0, "alpha", 0), - CO_FUTURE_WITH_STATEMENT) - -print_function = _Feature((2, 6, 0, "alpha", 2), - (3, 0, 0, "alpha", 0), - CO_FUTURE_PRINT_FUNCTION) - -unicode_literals = _Feature((2, 6, 0, "alpha", 2), - (3, 0, 0, "alpha", 0), - CO_FUTURE_UNICODE_LITERALS) diff --git a/python/testData/MockSdk2.7/Lib/numbers.py b/python/testData/MockSdk2.7/Lib/numbers.py deleted file mode 100644 index 2592643c1362..000000000000 --- a/python/testData/MockSdk2.7/Lib/numbers.py +++ /dev/null @@ -1,391 +0,0 @@ -# Copyright 2007 Google, Inc. All Rights Reserved. -# Licensed to PSF under a Contributor Agreement. - -"""Abstract Base Classes (ABCs) for numbers, according to PEP 3141. - -TODO: Fill out more detailed documentation on the operators.""" - -from __future__ import division -from abc import ABCMeta, abstractmethod, abstractproperty - -__all__ = ["Number", "Complex", "Real", "Rational", "Integral"] - -class Number(object): - """All numbers inherit from this class. - - If you just want to check if an argument x is a number, without - caring what kind, use isinstance(x, Number). - """ - __metaclass__ = ABCMeta - __slots__ = () - - # Concrete numeric types must provide their own hash implementation - __hash__ = None - - -## Notes on Decimal -## ---------------- -## Decimal has all of the methods specified by the Real abc, but it should -## not be registered as a Real because decimals do not interoperate with -## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But, -## abstract reals are expected to interoperate (i.e. R1 + R2 should be -## expected to work if R1 and R2 are both Reals). - -class Complex(Number): - """Complex defines the operations that work on the builtin complex type. - - In short, those are: a conversion to complex, .real, .imag, +, -, - *, /, abs(), .conjugate, ==, and !=. - - If it is given heterogenous arguments, and doesn't have special - knowledge about them, it should fall back to the builtin complex - type as described below. - """ - - __slots__ = () - - @abstractmethod - def __complex__(self): - """Return a builtin complex instance. Called for complex(self).""" - - # Will be __bool__ in 3.0. - def __nonzero__(self): - """True if self != 0. Called for bool(self).""" - return self != 0 - - @abstractproperty - def real(self): - """Retrieve the real component of this number. - - This should subclass Real. - """ - raise NotImplementedError - - @abstractproperty - def imag(self): - """Retrieve the imaginary component of this number. - - This should subclass Real. - """ - raise NotImplementedError - - @abstractmethod - def __add__(self, other): - """self + other""" - raise NotImplementedError - - @abstractmethod - def __radd__(self, other): - """other + self""" - raise NotImplementedError - - @abstractmethod - def __neg__(self): - """-self""" - raise NotImplementedError - - @abstractmethod - def __pos__(self): - """+self""" - raise NotImplementedError - - def __sub__(self, other): - """self - other""" - return self + -other - - def __rsub__(self, other): - """other - self""" - return -self + other - - @abstractmethod - def __mul__(self, other): - """self * other""" - raise NotImplementedError - - @abstractmethod - def __rmul__(self, other): - """other * self""" - raise NotImplementedError - - @abstractmethod - def __div__(self, other): - """self / other without __future__ division - - May promote to float. - """ - raise NotImplementedError - - @abstractmethod - def __rdiv__(self, other): - """other / self without __future__ division""" - raise NotImplementedError - - @abstractmethod - def __truediv__(self, other): - """self / other with __future__ division. - - Should promote to float when necessary. - """ - raise NotImplementedError - - @abstractmethod - def __rtruediv__(self, other): - """other / self with __future__ division""" - raise NotImplementedError - - @abstractmethod - def __pow__(self, exponent): - """self**exponent; should promote to float or complex when necessary.""" - raise NotImplementedError - - @abstractmethod - def __rpow__(self, base): - """base ** self""" - raise NotImplementedError - - @abstractmethod - def __abs__(self): - """Returns the Real distance from 0. Called for abs(self).""" - raise NotImplementedError - - @abstractmethod - def conjugate(self): - """(x+y*i).conjugate() returns (x-y*i).""" - raise NotImplementedError - - @abstractmethod - def __eq__(self, other): - """self == other""" - raise NotImplementedError - - def __ne__(self, other): - """self != other""" - # The default __ne__ doesn't negate __eq__ until 3.0. - return not (self == other) - -Complex.register(complex) - - -class Real(Complex): - """To Complex, Real adds the operations that work on real numbers. - - In short, those are: a conversion to float, trunc(), divmod, - %, <, <=, >, and >=. - - Real also provides defaults for the derived operations. - """ - - __slots__ = () - - @abstractmethod - def __float__(self): - """Any Real can be converted to a native float object. - - Called for float(self).""" - raise NotImplementedError - - @abstractmethod - def __trunc__(self): - """trunc(self): Truncates self to an Integral. - - Returns an Integral i such that: - * i>0 iff self>0; - * abs(i) <= abs(self); - * for any Integral j satisfying the first two conditions, - abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. - i.e. "truncate towards 0". - """ - raise NotImplementedError - - def __divmod__(self, other): - """divmod(self, other): The pair (self // other, self % other). - - Sometimes this can be computed faster than the pair of - operations. - """ - return (self // other, self % other) - - def __rdivmod__(self, other): - """divmod(other, self): The pair (self // other, self % other). - - Sometimes this can be computed faster than the pair of - operations. - """ - return (other // self, other % self) - - @abstractmethod - def __floordiv__(self, other): - """self // other: The floor() of self/other.""" - raise NotImplementedError - - @abstractmethod - def __rfloordiv__(self, other): - """other // self: The floor() of other/self.""" - raise NotImplementedError - - @abstractmethod - def __mod__(self, other): - """self % other""" - raise NotImplementedError - - @abstractmethod - def __rmod__(self, other): - """other % self""" - raise NotImplementedError - - @abstractmethod - def __lt__(self, other): - """self < other - - < on Reals defines a total ordering, except perhaps for NaN.""" - raise NotImplementedError - - @abstractmethod - def __le__(self, other): - """self <= other""" - raise NotImplementedError - - # Concrete implementations of Complex abstract methods. - def __complex__(self): - """complex(self) == complex(float(self), 0)""" - return complex(float(self)) - - @property - def real(self): - """Real numbers are their real component.""" - return +self - - @property - def imag(self): - """Real numbers have no imaginary component.""" - return 0 - - def conjugate(self): - """Conjugate is a no-op for Reals.""" - return +self - -Real.register(float) - - -class Rational(Real): - """.numerator and .denominator should be in lowest terms.""" - - __slots__ = () - - @abstractproperty - def numerator(self): - raise NotImplementedError - - @abstractproperty - def denominator(self): - raise NotImplementedError - - # Concrete implementation of Real's conversion to float. - def __float__(self): - """float(self) = self.numerator / self.denominator - - It's important that this conversion use the integer's "true" - division rather than casting one side to float before dividing - so that ratios of huge integers convert without overflowing. - - """ - return self.numerator / self.denominator - - -class Integral(Rational): - """Integral adds a conversion to long and the bit-string operations.""" - - __slots__ = () - - @abstractmethod - def __long__(self): - """long(self)""" - raise NotImplementedError - - def __index__(self): - """index(self)""" - return long(self) - - @abstractmethod - def __pow__(self, exponent, modulus=None): - """self ** exponent % modulus, but maybe faster. - - Accept the modulus argument if you want to support the - 3-argument version of pow(). Raise a TypeError if exponent < 0 - or any argument isn't Integral. Otherwise, just implement the - 2-argument version described in Complex. - """ - raise NotImplementedError - - @abstractmethod - def __lshift__(self, other): - """self << other""" - raise NotImplementedError - - @abstractmethod - def __rlshift__(self, other): - """other << self""" - raise NotImplementedError - - @abstractmethod - def __rshift__(self, other): - """self >> other""" - raise NotImplementedError - - @abstractmethod - def __rrshift__(self, other): - """other >> self""" - raise NotImplementedError - - @abstractmethod - def __and__(self, other): - """self & other""" - raise NotImplementedError - - @abstractmethod - def __rand__(self, other): - """other & self""" - raise NotImplementedError - - @abstractmethod - def __xor__(self, other): - """self ^ other""" - raise NotImplementedError - - @abstractmethod - def __rxor__(self, other): - """other ^ self""" - raise NotImplementedError - - @abstractmethod - def __or__(self, other): - """self | other""" - raise NotImplementedError - - @abstractmethod - def __ror__(self, other): - """other | self""" - raise NotImplementedError - - @abstractmethod - def __invert__(self): - """~self""" - raise NotImplementedError - - # Concrete implementations of Rational and Real abstract methods. - def __float__(self): - """float(self) == float(long(self))""" - return float(long(self)) - - @property - def numerator(self): - """Integers are their own numerators.""" - return +self - - @property - def denominator(self): - """Integers have a denominator of 1.""" - return 1 - -Integral.register(int) -Integral.register(long) diff --git a/python/testData/MockSdk2.7/python_stubs/cPickle.py b/python/testData/MockSdk2.7/python_stubs/cPickle.py deleted file mode 100644 index 79aaa3cbedf7..000000000000 --- a/python/testData/MockSdk2.7/python_stubs/cPickle.py +++ /dev/null @@ -1,126 +0,0 @@ -# encoding: utf-8 -# module cPickle -# from (built-in) -# by generator 1.147 -""" C implementation and optimization of the Python pickle module. """ - -# imports -import __builtin__ as __builtins__ # - -# Variables with simple values - -format_version = '2.0' - -HIGHEST_PROTOCOL = 2 - -__version__ = '1.71' - -# functions - -def dump(obj, file, protocol=0): # real signature unknown; restored from __doc__ - """ - dump(obj, file, protocol=0) -- Write an object in pickle format to the given file. - - See the Pickler docstring for the meaning of optional argument proto. - """ - pass - -def dumps(obj, protocol=0): # real signature unknown; restored from __doc__ - """ - dumps(obj, protocol=0) -- Return a string containing an object in pickle format. - - See the Pickler docstring for the meaning of optional argument proto. - """ - pass - -def load(file): # real signature unknown; restored from __doc__ - """ load(file) -- Load a pickle from the given file """ - pass - -def loads(string): # real signature unknown; restored from __doc__ - """ loads(string) -- Load a pickle from the given string """ - pass - -def Pickler(file, protocol=0): # real signature unknown; restored from __doc__ - """ - Pickler(file, protocol=0) -- Create a pickler. - - This takes a file-like object for writing a pickle data stream. - The optional proto argument tells the pickler to use the given - protocol; supported protocols are 0, 1, 2. The default - protocol is 0, to be backwards compatible. (Protocol 0 is the - only protocol that can be written to a file opened in text - mode and read back successfully. When using a protocol higher - than 0, make sure the file is opened in binary mode, both when - pickling and unpickling.) - - Protocol 1 is more efficient than protocol 0; protocol 2 is - more efficient than protocol 1. - - Specifying a negative protocol version selects the highest - protocol version supported. The higher the protocol used, the - more recent the version of Python needed to read the pickle - produced. - - The file parameter must have a write() method that accepts a single - string argument. It can thus be an open file object, a StringIO - object, or any other custom object that meets this interface. - """ - pass - -def Unpickler(file): # real signature unknown; restored from __doc__ - """ Unpickler(file) -- Create an unpickler. """ - pass - -# classes - -class PickleError(Exception): - # no doc - def __init__(self, *args, **kwargs): # real signature unknown - pass - - def __str__(self, *args, **kwargs): # real signature unknown - pass - - __weakref__ = property(lambda self: object(), lambda self, v: None, lambda self: None) # default - """list of weak references to the object (if defined)""" - - - -class UnpicklingError(PickleError): - # no doc - def __init__(self, *args, **kwargs): # real signature unknown - pass - - -class BadPickleGet(UnpicklingError): - # no doc - def __init__(self, *args, **kwargs): # real signature unknown - pass - - -class PicklingError(PickleError): - # no doc - def __init__(self, *args, **kwargs): # real signature unknown - pass - - -class UnpickleableError(PicklingError): - # no doc - def __init__(self, *args, **kwargs): # real signature unknown - pass - - def __str__(self, *args, **kwargs): # real signature unknown - pass - - -# variables with complex values - -compatible_formats = [ - '1.0', - '1.1', - '1.2', - '1.3', - '2.0', -] - diff --git a/python/testData/MockSdk3.7/Lib/numbers.py b/python/testData/MockSdk3.7/Lib/numbers.py deleted file mode 100644 index ed815ef41ebe..000000000000 --- a/python/testData/MockSdk3.7/Lib/numbers.py +++ /dev/null @@ -1,389 +0,0 @@ -# Copyright 2007 Google, Inc. All Rights Reserved. -# Licensed to PSF under a Contributor Agreement. - -"""Abstract Base Classes (ABCs) for numbers, according to PEP 3141. - -TODO: Fill out more detailed documentation on the operators.""" - -from abc import ABCMeta, abstractmethod - -__all__ = ["Number", "Complex", "Real", "Rational", "Integral"] - -class Number(metaclass=ABCMeta): - """All numbers inherit from this class. - - If you just want to check if an argument x is a number, without - caring what kind, use isinstance(x, Number). - """ - __slots__ = () - - # Concrete numeric types must provide their own hash implementation - __hash__ = None - - -## Notes on Decimal -## ---------------- -## Decimal has all of the methods specified by the Real abc, but it should -## not be registered as a Real because decimals do not interoperate with -## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But, -## abstract reals are expected to interoperate (i.e. R1 + R2 should be -## expected to work if R1 and R2 are both Reals). - -class Complex(Number): - """Complex defines the operations that work on the builtin complex type. - - In short, those are: a conversion to complex, .real, .imag, +, -, - *, /, abs(), .conjugate, ==, and !=. - - If it is given heterogeneous arguments, and doesn't have special - knowledge about them, it should fall back to the builtin complex - type as described below. - """ - - __slots__ = () - - @abstractmethod - def __complex__(self): - """Return a builtin complex instance. Called for complex(self).""" - - def __bool__(self): - """True if self != 0. Called for bool(self).""" - return self != 0 - - @property - @abstractmethod - def real(self): - """Retrieve the real component of this number. - - This should subclass Real. - """ - raise NotImplementedError - - @property - @abstractmethod - def imag(self): - """Retrieve the imaginary component of this number. - - This should subclass Real. - """ - raise NotImplementedError - - @abstractmethod - def __add__(self, other): - """self + other""" - raise NotImplementedError - - @abstractmethod - def __radd__(self, other): - """other + self""" - raise NotImplementedError - - @abstractmethod - def __neg__(self): - """-self""" - raise NotImplementedError - - @abstractmethod - def __pos__(self): - """+self""" - raise NotImplementedError - - def __sub__(self, other): - """self - other""" - return self + -other - - def __rsub__(self, other): - """other - self""" - return -self + other - - @abstractmethod - def __mul__(self, other): - """self * other""" - raise NotImplementedError - - @abstractmethod - def __rmul__(self, other): - """other * self""" - raise NotImplementedError - - @abstractmethod - def __truediv__(self, other): - """self / other: Should promote to float when necessary.""" - raise NotImplementedError - - @abstractmethod - def __rtruediv__(self, other): - """other / self""" - raise NotImplementedError - - @abstractmethod - def __pow__(self, exponent): - """self**exponent; should promote to float or complex when necessary.""" - raise NotImplementedError - - @abstractmethod - def __rpow__(self, base): - """base ** self""" - raise NotImplementedError - - @abstractmethod - def __abs__(self): - """Returns the Real distance from 0. Called for abs(self).""" - raise NotImplementedError - - @abstractmethod - def conjugate(self): - """(x+y*i).conjugate() returns (x-y*i).""" - raise NotImplementedError - - @abstractmethod - def __eq__(self, other): - """self == other""" - raise NotImplementedError - -Complex.register(complex) - - -class Real(Complex): - """To Complex, Real adds the operations that work on real numbers. - - In short, those are: a conversion to float, trunc(), divmod, - %, <, <=, >, and >=. - - Real also provides defaults for the derived operations. - """ - - __slots__ = () - - @abstractmethod - def __float__(self): - """Any Real can be converted to a native float object. - - Called for float(self).""" - raise NotImplementedError - - @abstractmethod - def __trunc__(self): - """trunc(self): Truncates self to an Integral. - - Returns an Integral i such that: - * i>0 iff self>0; - * abs(i) <= abs(self); - * for any Integral j satisfying the first two conditions, - abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. - i.e. "truncate towards 0". - """ - raise NotImplementedError - - @abstractmethod - def __floor__(self): - """Finds the greatest Integral <= self.""" - raise NotImplementedError - - @abstractmethod - def __ceil__(self): - """Finds the least Integral >= self.""" - raise NotImplementedError - - @abstractmethod - def __round__(self, ndigits=None): - """Rounds self to ndigits decimal places, defaulting to 0. - - If ndigits is omitted or None, returns an Integral, otherwise - returns a Real. Rounds half toward even. - """ - raise NotImplementedError - - def __divmod__(self, other): - """divmod(self, other): The pair (self // other, self % other). - - Sometimes this can be computed faster than the pair of - operations. - """ - return (self // other, self % other) - - def __rdivmod__(self, other): - """divmod(other, self): The pair (self // other, self % other). - - Sometimes this can be computed faster than the pair of - operations. - """ - return (other // self, other % self) - - @abstractmethod - def __floordiv__(self, other): - """self // other: The floor() of self/other.""" - raise NotImplementedError - - @abstractmethod - def __rfloordiv__(self, other): - """other // self: The floor() of other/self.""" - raise NotImplementedError - - @abstractmethod - def __mod__(self, other): - """self % other""" - raise NotImplementedError - - @abstractmethod - def __rmod__(self, other): - """other % self""" - raise NotImplementedError - - @abstractmethod - def __lt__(self, other): - """self < other - - < on Reals defines a total ordering, except perhaps for NaN.""" - raise NotImplementedError - - @abstractmethod - def __le__(self, other): - """self <= other""" - raise NotImplementedError - - # Concrete implementations of Complex abstract methods. - def __complex__(self): - """complex(self) == complex(float(self), 0)""" - return complex(float(self)) - - @property - def real(self): - """Real numbers are their real component.""" - return +self - - @property - def imag(self): - """Real numbers have no imaginary component.""" - return 0 - - def conjugate(self): - """Conjugate is a no-op for Reals.""" - return +self - -Real.register(float) - - -class Rational(Real): - """.numerator and .denominator should be in lowest terms.""" - - __slots__ = () - - @property - @abstractmethod - def numerator(self): - raise NotImplementedError - - @property - @abstractmethod - def denominator(self): - raise NotImplementedError - - # Concrete implementation of Real's conversion to float. - def __float__(self): - """float(self) = self.numerator / self.denominator - - It's important that this conversion use the integer's "true" - division rather than casting one side to float before dividing - so that ratios of huge integers convert without overflowing. - - """ - return self.numerator / self.denominator - - -class Integral(Rational): - """Integral adds a conversion to int and the bit-string operations.""" - - __slots__ = () - - @abstractmethod - def __int__(self): - """int(self)""" - raise NotImplementedError - - def __index__(self): - """Called whenever an index is needed, such as in slicing""" - return int(self) - - @abstractmethod - def __pow__(self, exponent, modulus=None): - """self ** exponent % modulus, but maybe faster. - - Accept the modulus argument if you want to support the - 3-argument version of pow(). Raise a TypeError if exponent < 0 - or any argument isn't Integral. Otherwise, just implement the - 2-argument version described in Complex. - """ - raise NotImplementedError - - @abstractmethod - def __lshift__(self, other): - """self << other""" - raise NotImplementedError - - @abstractmethod - def __rlshift__(self, other): - """other << self""" - raise NotImplementedError - - @abstractmethod - def __rshift__(self, other): - """self >> other""" - raise NotImplementedError - - @abstractmethod - def __rrshift__(self, other): - """other >> self""" - raise NotImplementedError - - @abstractmethod - def __and__(self, other): - """self & other""" - raise NotImplementedError - - @abstractmethod - def __rand__(self, other): - """other & self""" - raise NotImplementedError - - @abstractmethod - def __xor__(self, other): - """self ^ other""" - raise NotImplementedError - - @abstractmethod - def __rxor__(self, other): - """other ^ self""" - raise NotImplementedError - - @abstractmethod - def __or__(self, other): - """self | other""" - raise NotImplementedError - - @abstractmethod - def __ror__(self, other): - """other | self""" - raise NotImplementedError - - @abstractmethod - def __invert__(self): - """~self""" - raise NotImplementedError - - # Concrete implementations of Rational and Real abstract methods. - def __float__(self): - """float(self) == float(int(self))""" - return float(int(self)) - - @property - def numerator(self): - """Integers are their own numerators.""" - return +self - - @property - def denominator(self): - """Integers have a denominator of 1.""" - return 1 - -Integral.register(int) diff --git a/python/testSrc/com/jetbrains/python/PyIndexingTest.java b/python/testSrc/com/jetbrains/python/PyIndexingTest.java index d813ab50cbce..0179d08280d9 100644 --- a/python/testSrc/com/jetbrains/python/PyIndexingTest.java +++ b/python/testSrc/com/jetbrains/python/PyIndexingTest.java @@ -74,37 +74,44 @@ public class PyIndexingTest extends PyTestCase { public void testTodoIndexInLibs() { final String libraryWithTodoName = "numbers.py"; - final List indexFiles = getTodoFiles(myFixture.getProject()); - // project file in the TodoIndex - assertTrue(indexFiles.stream().anyMatch((x) -> "a.py".equals(x.getName()))); + runWithAdditionalFileInLibDir( + libraryWithTodoName, + "# TODO: this should be updated", + (__) -> { + final List indexFiles = getTodoFiles(myFixture.getProject()); - // no library files in the TodoIndex - assertFalse(indexFiles.stream().anyMatch((x) -> libraryWithTodoName.equals(x.getName()))); + // project file in the TodoIndex + assertTrue(indexFiles.stream().anyMatch((x) -> "a.py".equals(x.getName()))); - final Module module = myFixture.getModule(); - final Sdk sdk = PythonSdkUtil.findPythonSdk(module); + // no library files in the TodoIndex + assertFalse(indexFiles.stream().anyMatch((x) -> libraryWithTodoName.equals(x.getName()))); - final VirtualFile libDir = PySearchUtilBase.findLibDir(sdk); + final Module module = myFixture.getModule(); + final Sdk sdk = PythonSdkUtil.findPythonSdk(module); - ModuleRootModificationUtil.addContentRoot(module, libDir); - try { - // mock sdk doesn't fire events - FileBasedIndex.getInstance().requestRebuild(TodoIndex.NAME); - FileBasedIndex.getInstance().ensureUpToDate(TodoIndex.NAME, myFixture.getProject(), null); - final List updatedIndexFiles = getTodoFiles(myFixture.getProject()); - // but if it is added as a content root - it should be in the TodoIndex - assertTrue(updatedIndexFiles.stream().anyMatch((x) -> libraryWithTodoName.equals(x.getName()))); - } - finally { - ModuleRootModificationUtil.updateModel(module, model -> { - for (ContentEntry entry : model.getContentEntries()) { - if (libDir.equals(entry.getFile())) { - model.removeContentEntry(entry); - } + final VirtualFile libDir = PySearchUtilBase.findLibDir(sdk); + + ModuleRootModificationUtil.addContentRoot(module, libDir); + try { + // mock sdk doesn't fire events + FileBasedIndex.getInstance().requestRebuild(TodoIndex.NAME); + FileBasedIndex.getInstance().ensureUpToDate(TodoIndex.NAME, myFixture.getProject(), null); + final List updatedIndexFiles = getTodoFiles(myFixture.getProject()); + // but if it is added as a content root - it should be in the TodoIndex + assertTrue(updatedIndexFiles.stream().anyMatch((x) -> libraryWithTodoName.equals(x.getName()))); } - }); - } + finally { + ModuleRootModificationUtil.updateModel(module, model -> { + for (ContentEntry entry : model.getContentEntries()) { + if (libDir.equals(entry.getFile())) { + model.removeContentEntry(entry); + } + } + }); + } + } + ); } // PY-19047 diff --git a/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts b/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts index 9568c0f3e27f..27d60f8a98d6 100644 --- a/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts +++ b/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts @@ -26,6 +26,7 @@ sync(repo, bundled) val whiteList = setOf( "__builtin__", + "__future__", "_importlib_modulespec", "_io", "abc", @@ -35,6 +36,7 @@ val whiteList = setOf( "builtins", "collections", "concurrent", + "cPickle", "crypt", "ctypes", "datetime", @@ -47,6 +49,7 @@ val whiteList = setOf( "math", "mock", "multiprocessing", + "numbers", "pathlib", "queue", "re",