Mercurial > public > mercurial-scm > hg
comparison mercurial/encoding.py @ 51718:43adbe03079b
typing: add type hints to the `charencode` module
Since this module is dynamically imported from either `mercurial.pure` or
`mercurial.cext`, these hints aren't detected in `mercurial.encoding`, and need
to be imported directly there during the type-checking phase. This keeps the
runtime selection via the policy config in place, but allows pytype to see these
as functions with proper signatures instead of just `Any`. We don't attempt to
import the `mercurial.cext` version yet because there's no types stubs for that
module, but this will get the ball rolling.
I thought this would spill over into other modules from there, but the only two
*.pyi files that changed were for `encoding` and `charencode`. Applying this to
other dynamically selected modules will clean some things up in other files, so
this is a start. I had originally redefined the functions in the type-checking
block (like some of the `os.path` aliasing in `mercurial.util`), but this is
better because we won't have another duplication of the definitions that may get
out of date.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 19 Jul 2024 20:09:48 -0400 |
parents | ca7bde5dbafb |
children | f4733654f144 |
comparison
equal
deleted
inserted
replaced
51717:ed28085827ec | 51718:43adbe03079b |
---|---|
37 _jsonescapeu8fast = charencode.jsonescapeu8fast | 37 _jsonescapeu8fast = charencode.jsonescapeu8fast |
38 | 38 |
39 _sysstr = pycompat.sysstr | 39 _sysstr = pycompat.sysstr |
40 | 40 |
41 unichr = chr | 41 unichr = chr |
42 | |
43 if typing.TYPE_CHECKING: | |
44 # TODO: make a stub file for .cext.charencode, and import here | |
45 from .pure.charencode import ( | |
46 asciilower, | |
47 asciiupper, | |
48 isasciistr, | |
49 jsonescapeu8fast as _jsonescapeu8fast, | |
50 ) | |
51 | |
42 | 52 |
43 # These unicode characters are ignored by HFS+ (Apple Technote 1150, | 53 # These unicode characters are ignored by HFS+ (Apple Technote 1150, |
44 # "Unicode Subtleties"), so we need to ignore them in some places for | 54 # "Unicode Subtleties"), so we need to ignore them in some places for |
45 # sanity. | 55 # sanity. |
46 _ignore = [ | 56 _ignore = [ |
522 lower = -1 | 532 lower = -1 |
523 upper = 1 | 533 upper = 1 |
524 other = 0 | 534 other = 0 |
525 | 535 |
526 | 536 |
527 def jsonescape(s: Any, paranoid: Any = False) -> Any: | 537 def jsonescape(s: bytes, paranoid: bool = False) -> bytes: |
528 """returns a string suitable for JSON | 538 """returns a string suitable for JSON |
529 | 539 |
530 JSON is problematic for us because it doesn't support non-Unicode | 540 JSON is problematic for us because it doesn't support non-Unicode |
531 bytes. To deal with this, we take the following approach: | 541 bytes. To deal with this, we take the following approach: |
532 | 542 |