Skip to content

Commit e2d772b

Browse files
committed
Add default type hint for bytes
1 parent 46d380b commit e2d772b

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/python.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface DataTypeContainer {
2727
export const getDataTypeContainer = (): DataTypeContainer => {
2828
return {
2929
bool: new DataType(PythonType.Bool, typeCategories.bool),
30+
bytes: new DataType(PythonType.Bytes, typeCategories.bytes),
3031
complex: new DataType(PythonType.Complex, typeCategories.complex),
3132
dict: new DataType(PythonType.Dict, typeCategories.dict),
3233
float: new DataType(PythonType.Float, typeCategories.float),
@@ -44,6 +45,7 @@ export const getDataTypeContainer = (): DataTypeContainer => {
4445
*/
4546
export enum PythonType {
4647
Bool = "bool",
48+
Bytes = "bytes",
4749
Complex = "complex",
4850
Dict = "dict",
4951
Float = "float",
@@ -69,6 +71,7 @@ export enum TypeCategory {
6971
*/
7072
const typeCategories: { [key: string]: TypeCategory } = {
7173
bool: TypeCategory.Basic,
74+
bytes: TypeCategory.Basic,
7275
complex: TypeCategory.Basic,
7376
dict: TypeCategory.Collection,
7477
float: TypeCategory.Basic,

src/typeSearch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export class TypeSearch {
130130
[ PythonType.Set, `${PythonType.Set}\\(`, `^ *{( *"[^"]*" *[},]+| *'[^']*' *[},]+|[^:]+[}])`],
131131
[ PythonType.Dict, `${PythonType.Dict}\\(`, "^ *{"],
132132
[ PythonType.String, `${PythonType.String}\\(`, `^ *(['"]{2}|(\\( *)?"[^"]*"|(\\( *)?'[^']*')`],
133+
[ PythonType.Bytes, `${PythonType.Bytes}\\(`, `^ *b['"]`],
133134
[ PythonType.Int, `${PythonType.Int}\\(`, `^ *[-(]*[0-9]`],
134135
[ PythonType.Object, `${PythonType.Object}\\(` ]
135136
];

test/suite/typeSearch/detection.test.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,37 @@ suite('TypeSearch.detectType', () => {
141141
actual = TypeSearch.detectType(src);
142142
assert.strictEqual(TypeSearch.detectType(src), expected, messageFor(src, expected, actual));
143143
});
144+
145+
test("detects bytes", async () => {
146+
const expected = "bytes";
147+
148+
let src = "b'dont return string please'";
149+
let actual = TypeSearch.detectType(src);
150+
assert.strictEqual(actual, expected);
151+
152+
src = 'b"dont return string please"';
153+
actual = TypeSearch.detectType(src);
154+
assert.strictEqual(TypeSearch.detectType(src), expected, messageFor(src, expected, actual));
155+
156+
src = "b'''hi'''";
157+
actual = TypeSearch.detectType(src);
158+
assert.strictEqual(TypeSearch.detectType(src), expected, messageFor(src, expected, actual));
159+
160+
src = 'b"""hi"""';
161+
actual = TypeSearch.detectType(src);
162+
assert.strictEqual(TypeSearch.detectType(src), expected, messageFor(src, expected, actual));
163+
});
144164

145165
test("detects type() call", () => {
146166
const testCases: TestCase[] = [
147-
{ data: "int('2')", expected: "int"},
148-
{ data: "bool(true)", expected: "bool"},
149-
{ data: "list(foo)", expected: "list"},
150-
{ data: "dict(foo)", expected: "dict"},
151-
{ data: "tuple(foo)", expected: "tuple"},
152-
{ data: "str(1)", expected: "str"},
153-
{ data: "set([1])", expected: "set"}
167+
{ data: "int('2')", expected: "int" },
168+
{ data: "bool('true')", expected: "bool" },
169+
{ data: "list(foo)", expected: "list" },
170+
{ data: "dict(foo)", expected: "dict" },
171+
{ data: "tuple(foo)", expected: "tuple" },
172+
{ data: "str(1)", expected: "str" },
173+
{ data: "set([1])", expected: "set" },
174+
{ data: "bytes('hi', encoding='utf-8')", expected: "bytes" }
154175
];
155176
for (const c of testCases) {
156177
let actual = TypeSearch.detectType(c.data);

0 commit comments

Comments
 (0)