Mercurial > hg
comparison mercurial/templater.py @ 51678:0e16efe30866
typing: add a few trivial type hints to `mercurial/templater.py`
Since hg 3dbc7b1ecaba, pytype started inferring that the second value in the
tuple is `BinaryIO`, but still hasn't been able to figure out the rest of
`open_template()`. We can be more precise.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Wed, 10 Jul 2024 18:34:47 -0400 |
parents | 18c8c18993f0 |
children |
comparison
equal
deleted
inserted
replaced
51677:df6ce326936f | 51678:0e16efe30866 |
---|---|
66 """ | 66 """ |
67 | 67 |
68 | 68 |
69 import abc | 69 import abc |
70 import os | 70 import os |
71 | |
72 from typing import ( | |
73 BinaryIO, | |
74 Optional, | |
75 Tuple, | |
76 ) | |
71 | 77 |
72 from .i18n import _ | 78 from .i18n import _ |
73 from .pycompat import ( | 79 from .pycompat import ( |
74 FileNotFoundError, | 80 FileNotFoundError, |
75 ) | 81 ) |
1119 '''return the directory used for template files, or None.''' | 1125 '''return the directory used for template files, or None.''' |
1120 path = os.path.normpath(os.path.join(resourceutil.datapath, b'templates')) | 1126 path = os.path.normpath(os.path.join(resourceutil.datapath, b'templates')) |
1121 return path if os.path.isdir(path) else None | 1127 return path if os.path.isdir(path) else None |
1122 | 1128 |
1123 | 1129 |
1124 def open_template(name, templatepath=None): | 1130 def open_template( |
1131 name: bytes, templatepath: Optional[bytes] = None | |
1132 ) -> Tuple[bytes, BinaryIO]: | |
1125 """returns a file-like object for the given template, and its full path | 1133 """returns a file-like object for the given template, and its full path |
1126 | 1134 |
1127 If the name is a relative path and we're in a frozen binary, the template | 1135 If the name is a relative path and we're in a frozen binary, the template |
1128 will be read from the mercurial.templates package instead. The returned path | 1136 will be read from the mercurial.templates package instead. The returned path |
1129 will then be the relative path. | 1137 will then be the relative path. |
1154 name, | 1162 name, |
1155 resourceutil.open_resource(package_name, name_parts[-1]), | 1163 resourceutil.open_resource(package_name, name_parts[-1]), |
1156 ) | 1164 ) |
1157 | 1165 |
1158 | 1166 |
1159 def try_open_template(name, templatepath=None): | 1167 def try_open_template( |
1168 name: bytes, templatepath: Optional[bytes] = None | |
1169 ) -> Tuple[Optional[bytes], Optional[BinaryIO]]: | |
1160 try: | 1170 try: |
1161 return open_template(name, templatepath) | 1171 return open_template(name, templatepath) |
1162 except (EnvironmentError, ImportError): | 1172 except (EnvironmentError, ImportError): |
1163 return None, None | 1173 return None, None |