mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
* Render CPython defaults in generated text signatures Arguments whose default rendered as `<unrepresentable>`, while the same callable has a representable default in CPython, now render that default: - Where the Rust value can be a literal, the argument becomes a plain field with `default = <literal>`. - The remaining cases keep their Rust default and add `py_default` with a comment. These are wrappers such as ArgIntoFloat or ArgIntoBool, values that need the VM, enums, and Option where None means another value. Byte string literals render as Python bytes. A function registered under several names gets an internal doc per name, so its __doc__ no longer carries the signature header. Removed expectedFailure from test_pydoc tests that now pass. Assisted-by: Grok:grok-4.7 Assisted-by: Claude:claude-opus-5-5 * Match native callable signatures to CPython names and kinds Rename parameters and adjust positional-only / keyword-only kinds and defaults of native functions and methods so their generated text signatures match CPython. Remove expectedFailure markers from tests that now pass. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Fix Windows console read size and keep type.__prepare__ permissive Assisted-by: Claude:claude-opus-5-5 * Derive signature defaults from typed Rust values Param.default is now a DefaultRepr rendered by the const writer. Literals, integer and bool constants become typed defaults, and other default expressions use the type's const fn py_default. A bare `optional` renders as None, so `default = None` rewrites are reverted. Address review comments: zlib max_length defaults, get_ca_certs binary_form keyword, getpeercert der, WASM ssl certificate keywords, fstatvfs fd-only, and _sre endpos shown as sys.maxsize. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Generate class constructor text signatures at compile time A #[pyclass] impl composes the type's internal doc from the Args of its Initializer or Constructor, so type.__text_signature__ and inspect.signature work for native classes. __doc__ stays the plain doc. Struct sequences get (iterable=(), /) and __reduce__ (self, /). Hand-parsed constructors are converted to FromArgs structs, and _weakref.proxy is split from ProxyType. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Remove expectedFailure from test_hmac tests that now pass Assisted-by: Claude:claude-opus-5-5 * Match remaining native signatures and count missing CPython signatures as matches Give lock.__enter__ and RLock.__enter__ their own no-argument methods, add FromArgs for start_new_thread and start_joinable_thread keeping their errors, take RTLD_GLOBAL/RTLD_LOCAL from libc, and align names, kinds and <unrepresentable> defaults where CPython's signature is invalid (bytes.hex, reduce, warn, link, setlocale, add_done_callback, ...). getaddrinfo accepts keyword arguments. whats_left treats a callable CPython has no signature for as matching. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Drop unfulfilled dead_code expectation on os.link dir_fd args Assisted-by: Claude:claude-opus-5-5 * Distinguish OptionalArg and Option in signature defaults An `optional` field's signature default now comes from its type: Option<T> renders None, while OptionalArg<T> (a missing argument that is not any Python value) renders <unrepresentable>. Fields where a missing argument behaves exactly like None become Option<T>; fields whose missing state differs keep OptionalArg. Redundant py_default = "<unrepresentable>" entries are removed. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Remove expectedFailure from test_rlcompleter.test_attr_matches Assisted-by: Claude:claude-opus-5-5 * Use Option for optional arguments whose missing and None states match OptionalOption and OptionalArg<Option<T>> fields that treat a missing argument like None become Option<T> with #[pyarg(optional)], which renders =None in the text signature. Plain optional parameters of that kind move into small FromArgs structs. Uses where missing and None differ (reduce initial, StringIO newline, TextIOWrapper.reconfigure, set_asyncgen_hooks) keep OptionalOption. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Render ::NAME defaults by name and restore named constant defaults - default = ::NAME writes NAME into the text signature and stores Into::into(NAME) as the Rust value. - A one-field tuple-struct argument pattern such as Fildes(fd) names the parameter after its binding, so fcntl and termios keep destructuring. - zlib, lzma, syslog, mmap and select defaults use their named constants again instead of literals or <unrepresentable>. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Name FromArgs fields after their Python parameters Drop #[pyarg(name = ...)] where the Python name is a valid Rust field name and rename the field instead. The option stays only for keywords (fn, loop, final, type, self) and the non-snake_case memLevel. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Build literal defaults from Python values and type <unrepresentable> - default = LIT on a non-primitive field converts the literal through ToPyObject and TryFromObject when the argument is missing, so object fields no longer need a vm expression plus a matching py_default. - A bare default on a primitive integer, bool or float field renders its zero value. - DefaultRepr::Unrepresentable replaces the <unrepresentable> text, and py_default = "<unrepresentable>" is a compile error. - pyexpat.ParserCreate distinguishes a missing intern from intern=None, mmap AccessMode renders its value, and sqlite blobopen names rowid. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Resolve native docstrings from the doc database at compile time - rustpython-doc stores its table as a sorted const slice with const fn lookups (get, get_attr, class_attr_doc) instead of a phf map, and the update-doc-db workflow emits that form. - Class, method, getset, module function and struct sequence docs, and per-class slot wrapper docs, are chosen by const lookups in the derive output. The database text wins over Rust doc comments. - Macros reach the table through rustpython_vm::__exports, so only the vm depends on rustpython-doc. - Remove expectedFailure markers from doctest and pydoc tests that pass. Assisted-by: Claude:claude-opus-5-5 Assisted-by: Grok:grok-4.7 * Keep time.gmtime, localtime and ctime without a text signature Their [seconds] parameter has no text signature, and unittest.mock relies on inspect.signature failing for them. Take OptionalOption again so the default stays unrepresentable, and treat None like a missing argument. Assisted-by: Claude:claude-opus-5-5
240 lines
9.8 KiB
Python
Vendored
240 lines
9.8 KiB
Python
Vendored
import unittest
|
|
from unittest.mock import patch
|
|
import builtins
|
|
import types
|
|
import rlcompleter
|
|
from test.support import MISSING_C_DOCSTRINGS
|
|
|
|
class CompleteMe:
|
|
""" Trivial class used in testing rlcompleter.Completer. """
|
|
spam = 1
|
|
_ham = 2
|
|
|
|
|
|
class TestRlcompleter(unittest.TestCase):
|
|
def setUp(self):
|
|
self.stdcompleter = rlcompleter.Completer()
|
|
self.completer = rlcompleter.Completer(dict(spam=int,
|
|
egg=str,
|
|
CompleteMe=CompleteMe))
|
|
|
|
# forces stdcompleter to bind builtins namespace
|
|
self.stdcompleter.complete('', 0)
|
|
|
|
def test_namespace(self):
|
|
class A(dict):
|
|
pass
|
|
class B(list):
|
|
pass
|
|
|
|
self.assertTrue(self.stdcompleter.use_main_ns)
|
|
self.assertFalse(self.completer.use_main_ns)
|
|
self.assertFalse(rlcompleter.Completer(A()).use_main_ns)
|
|
self.assertRaises(TypeError, rlcompleter.Completer, B((1,)))
|
|
|
|
def test_global_matches(self):
|
|
# test with builtins namespace
|
|
self.assertEqual(sorted(self.stdcompleter.global_matches('di')),
|
|
[x+'(' for x in dir(builtins) if x.startswith('di')])
|
|
self.assertEqual(sorted(self.stdcompleter.global_matches('st')),
|
|
[x+'(' for x in dir(builtins) if x.startswith('st')])
|
|
self.assertEqual(self.stdcompleter.global_matches('akaksajadhak'), [])
|
|
|
|
# test with a customized namespace
|
|
self.assertEqual(self.completer.global_matches('CompleteM'),
|
|
['CompleteMe(' if MISSING_C_DOCSTRINGS else 'CompleteMe()'])
|
|
self.assertEqual(self.completer.global_matches('eg'),
|
|
['egg('])
|
|
# XXX: see issue5256
|
|
self.assertEqual(self.completer.global_matches('CompleteM'),
|
|
['CompleteMe(' if MISSING_C_DOCSTRINGS else 'CompleteMe()'])
|
|
|
|
def test_attr_matches(self):
|
|
# test with builtins namespace
|
|
self.assertEqual(self.stdcompleter.attr_matches('str.s'),
|
|
['str.{}('.format(x) for x in dir(str)
|
|
if x.startswith('s')])
|
|
self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), [])
|
|
|
|
def create_expected_for_none():
|
|
if not MISSING_C_DOCSTRINGS:
|
|
parentheses = ('__init_subclass__', '__class__')
|
|
else:
|
|
# When `--without-doc-strings` is used, `__class__`
|
|
# won't have a known signature.
|
|
parentheses = ('__init_subclass__',)
|
|
|
|
items = set()
|
|
for x in dir(None):
|
|
if x in parentheses:
|
|
items.add(f'None.{x}()')
|
|
elif x == '__doc__':
|
|
items.add(f'None.{x}')
|
|
else:
|
|
items.add(f'None.{x}(')
|
|
return sorted(items)
|
|
|
|
expected = create_expected_for_none()
|
|
self.assertEqual(self.stdcompleter.attr_matches('None.'), expected)
|
|
self.assertEqual(self.stdcompleter.attr_matches('None._'), expected)
|
|
self.assertEqual(self.stdcompleter.attr_matches('None.__'), expected)
|
|
|
|
# test with a customized namespace
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.sp'),
|
|
['CompleteMe.spam'])
|
|
self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.'),
|
|
['CompleteMe.mro()', 'CompleteMe.spam'])
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe._'),
|
|
['CompleteMe._ham'])
|
|
matches = self.completer.attr_matches('CompleteMe.__')
|
|
for x in matches:
|
|
self.assertStartsWith(x, 'CompleteMe.__')
|
|
self.assertIn('CompleteMe.__name__', matches)
|
|
self.assertIn('CompleteMe.__new__(', matches)
|
|
|
|
with patch.object(CompleteMe, "me", CompleteMe, create=True):
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
|
|
['CompleteMe.me.me.spam'])
|
|
self.assertEqual(self.completer.attr_matches('egg.s'),
|
|
['egg.{}('.format(x) for x in dir(str)
|
|
if x.startswith('s')])
|
|
|
|
def test_excessive_getattr(self):
|
|
"""Ensure getattr() is invoked no more than once per attribute"""
|
|
|
|
# note the special case for @property methods below; that is why
|
|
# we use __dir__ and __getattr__ in class Foo to create a "magic"
|
|
# class attribute 'bar'. This forces `getattr` to call __getattr__
|
|
# (which is doesn't necessarily do).
|
|
class Foo:
|
|
calls = 0
|
|
bar = ''
|
|
def __getattribute__(self, name):
|
|
if name == 'bar':
|
|
self.calls += 1
|
|
return None
|
|
return super().__getattribute__(name)
|
|
|
|
f = Foo()
|
|
completer = rlcompleter.Completer(dict(f=f))
|
|
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
|
|
self.assertEqual(f.calls, 1)
|
|
|
|
def test_property_method_not_called(self):
|
|
class Foo:
|
|
_bar = 0
|
|
property_called = False
|
|
|
|
@property
|
|
def bar(self):
|
|
self.property_called = True
|
|
return self._bar
|
|
|
|
f = Foo()
|
|
completer = rlcompleter.Completer(dict(f=f))
|
|
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
|
|
self.assertFalse(f.property_called)
|
|
|
|
def test_released_memoryview_completion_works(self):
|
|
mv = memoryview(b"abc")
|
|
mv.release()
|
|
|
|
self.assertIsInstance(type(mv).shape, types.GetSetDescriptorType)
|
|
self.assertIsInstance(type(mv).strides, types.GetSetDescriptorType)
|
|
|
|
completer = rlcompleter.Completer(dict(mv=mv))
|
|
matches = completer.attr_matches('mv.')
|
|
|
|
# These are getset descriptors on memoryview and should be completed
|
|
# without evaluating the released-memoryview getters.
|
|
self.assertIn('mv.shape', matches)
|
|
self.assertIn('mv.strides', matches)
|
|
|
|
def test_member_descriptor_not_evaluated(self):
|
|
class Foo:
|
|
__slots__ = ("boom",)
|
|
boom_accesses = 0
|
|
|
|
def __getattribute__(self, name):
|
|
if name == "boom":
|
|
type(self).boom_accesses += 1
|
|
raise RuntimeError("boom access should be skipped")
|
|
return super().__getattribute__(name)
|
|
|
|
self.assertIsInstance(Foo.boom, types.MemberDescriptorType)
|
|
|
|
completer = rlcompleter.Completer(dict(f=Foo()))
|
|
matches = completer.attr_matches('f.')
|
|
self.assertIn('f.boom', matches)
|
|
self.assertEqual(Foo.boom_accesses, 0)
|
|
|
|
def test_raising_descriptor_completion_works(self):
|
|
class ExplodingDescriptor:
|
|
def __init__(self):
|
|
self.instance_get_calls = 0
|
|
|
|
def __get__(self, obj, owner):
|
|
if obj is None:
|
|
return self
|
|
self.instance_get_calls += 1
|
|
raise RuntimeError("descriptor getter exploded")
|
|
|
|
class Foo:
|
|
boom = ExplodingDescriptor()
|
|
|
|
completer = rlcompleter.Completer(dict(f=Foo()))
|
|
matches = completer.attr_matches('f.')
|
|
self.assertIn('f.boom', matches)
|
|
self.assertEqual(Foo.boom.instance_get_calls, 0)
|
|
|
|
def test_uncreated_attr(self):
|
|
# Attributes like properties and slots should be completed even when
|
|
# they haven't been created on an instance
|
|
class Foo:
|
|
__slots__ = ("bar",)
|
|
completer = rlcompleter.Completer(dict(f=Foo()))
|
|
self.assertEqual(completer.complete('f.', 0), 'f.bar')
|
|
|
|
@unittest.mock.patch('rlcompleter._readline_available', False)
|
|
def test_complete(self):
|
|
completer = rlcompleter.Completer()
|
|
self.assertEqual(completer.complete('', 0), '\t')
|
|
self.assertEqual(completer.complete('a', 0), 'and ')
|
|
self.assertEqual(completer.complete('a', 1), 'as ')
|
|
self.assertEqual(completer.complete('as', 2), 'assert ')
|
|
self.assertEqual(completer.complete('an', 0), 'and ')
|
|
self.assertEqual(completer.complete('pa', 0), 'pass')
|
|
self.assertEqual(completer.complete('Fa', 0), 'False')
|
|
self.assertEqual(completer.complete('el', 0), 'elif ')
|
|
self.assertEqual(completer.complete('el', 1), 'else')
|
|
self.assertEqual(completer.complete('tr', 0), 'try:')
|
|
self.assertEqual(completer.complete('_', 0), '_')
|
|
self.assertEqual(completer.complete('match', 0), 'match ')
|
|
self.assertEqual(completer.complete('case', 0), 'case ')
|
|
|
|
def test_duplicate_globals(self):
|
|
namespace = {
|
|
'False': None, # Keyword vs builtin vs namespace
|
|
'assert': None, # Keyword vs namespace
|
|
'try': lambda: None, # Keyword vs callable
|
|
'memoryview': None, # Callable builtin vs non-callable
|
|
'Ellipsis': lambda: None, # Non-callable builtin vs callable
|
|
}
|
|
completer = rlcompleter.Completer(namespace)
|
|
self.assertEqual(completer.complete('False', 0), 'False')
|
|
self.assertIsNone(completer.complete('False', 1)) # No duplicates
|
|
# Space or colon added due to being a reserved keyword
|
|
self.assertEqual(completer.complete('assert', 0), 'assert ')
|
|
self.assertIsNone(completer.complete('assert', 1))
|
|
self.assertEqual(completer.complete('try', 0), 'try:')
|
|
self.assertIsNone(completer.complete('try', 1))
|
|
# No opening bracket "(" because we overrode the built-in class
|
|
self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
|
|
self.assertIsNone(completer.complete('memoryview', 1))
|
|
self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis()')
|
|
self.assertIsNone(completer.complete('Ellipsis', 1))
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|