8000 Pylint fixes for botbuilder-ai library code and tests (#272) · aiedward/botbuilder-python@0bfda9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bfda9e

Browse files
authored
Pylint fixes for botbuilder-ai library code and tests (microsoft#272)
1 parent 9546e07 commit 0bfda9e

12 files changed

+148
-138
lines changed

libraries/botbuilder-ai/botbuilder/ai/luis/activity_util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313

14-
class ActivityUtil(object):
14+
class ActivityUtil:
1515
@staticmethod
1616
def create_trace(
1717
turn_activity: Activity,
@@ -26,11 +26,14 @@ def create_trace(
2626
:type turn_activity: Activity
2727
:param name: The value to assign to the trace activity's <see cref="Activity.name"/> property.
2828
:type name: str
29-
:param value: The value to assign to the trace activity's <see cref="Activity.value"/> property., defaults to None
29+
:param value: The value to assign to the trace activity's <see cref="Activity.value"/> property., defaults
30+
to None
3031
:param value: object, optional
31-
:param value_type: The value to assign to the trace activity's <see cref="Activity.value_type"/> property, defaults to None
32+
:param value_type: The value to assign to the trace activity's <see cref="Activity.value_type"/> property,
33+
defaults to None
3234
:param value_type: str, optional
33-
:param label: The value to assign to the trace activity's <see cref="Activity.label"/> property, defaults to None
35+
:param label: The value to assign to the trace activity's <see cref="Activity.label"/> property, defaults
36+
to None
3437
:param label: str, optional
3538
:return: The created trace activity.
3639
:rtype: Activity

libraries/botbuilder-ai/botbuilder/ai/luis/luis_application.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77
from uuid import UUID, uuid4
88

99

10-
class LuisApplication(object):
10+
class LuisApplication:
1111
"""
1212
Data describing a LUIS application.
1313
"""
1414

1515
def __init__(self, application_id: str, endpoint_key: str, endpoint: str):
1616
"""Initializes a new instance of the <see cref="LuisApplication"/> class.
17-
1817
:param application_id: LUIS application ID.
1918
:type application_id: str
2019
:param endpoint_key: LUIS subscription or endpoint key.
2120
:type endpoint_key: str
2221
:param endpoint: LUIS endpoint to use like https://westus.api.cognitive.microsoft.com.
2322
:type endpoint: str
24-
:raises ValueError:
23+
:raises ValueError:
2524
:raises ValueError:
2625
:raises ValueError:
2726
"""
@@ -48,7 +47,6 @@ def __init__(self, application_id: str, endpoint_key: str, endpoint: str):
4847
@classmethod
4948
def from_application_endpoint(cls, application_endpoint: str):
5049
"""Initializes a new instance of the <see cref="LuisApplication"/> class.
51-
5250
:param application_endpoint: LUIS application endpoint.
5351
:type application_endpoint: str
5452
:return:

libraries/botbuilder-ai/botbuilder/ai/luis/luis_prediction_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from botbuilder.core import BotTelemetryClient, NullTelemetryClient
55

66

7-
class LuisPredictionOptions(object):
7+
class LuisPredictionOptions:
88
"""
99
Optional parameters for a LUIS prediction request.
1010
"""

libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@
1010

1111
from botbuilder.core import (
1212
BotAssert,
13-
BotTelemetryClient,
1413
IntentScore,
15-
NullTelemetryClient,
1614
RecognizerResult,
1715
TurnContext,
1816
)
19-
from botbuilder.schema import Activity, ActivityTypes, ChannelAccount
17+
from botbuilder.schema import ActivityTypes
2018

2119
from . import LuisApplication, LuisPredictionOptions, LuisTelemetryConstants
2220
from .activity_util import ActivityUtil
2321
from .luis_util import LuisUtil
2422

2523

26-
class LuisRecognizer(object):
24+
class LuisRecognizer:
2725
"""
2826
A LUIS based implementation of <see cref="IRecognizer"/>.
2927
"""
@@ -41,7 +39,6 @@ def __init__(
4139
include_api_results: bool = False,
4240
):
4341
"""Initializes a new instance of the <see cref="LuisRecognizer"/> class.
44-
4542
:param application: The LUIS application to use to recognize text.
4643
:type application: LuisApplication
4744
:param prediction_options: The LUIS prediction options to use, defaults to None
@@ -77,12 +74,12 @@ def top_intent(
7774
results: RecognizerResult, default_intent: str = "None", min_score: float = 0.0
7875
) -> str:
7976
"""Returns the name of the top scoring intent from a set of LUIS results.
80-
8177
:param results: Result set to be searched.
8278
:type results: RecognizerResult
8379
:param default_intent: Intent name to return should a top intent be found, defaults to "None"
8480
:param default_intent: str, optional
85-
:param min_score: Minimum score needed for an intent to be considered as a top intent. If all intents in the set are below this threshold then the `defaultIntent` will be returned, defaults to 0.0
81+
:param min_score: Minimum score needed for an intent to be considered as a top intent. If all intents in the
82+
set are below this threshold then the `defaultIntent` will be returned, defaults to 0.0
8683
:param min_score: float, optional
8784
:raises TypeError:
8885
:return: The top scoring intent name.
@@ -111,12 +108,13 @@ async def recognize(
111108
luis_prediction_options: LuisPredictionOptions = None,
112109
) -> RecognizerResult:
113110
"""Return results of the analysis (Suggested actions and intents).
114-
115111
:param turn_context: Context object containing information for a single turn of conversation with a user.
116112
:type turn_context: TurnContext
117-
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults to None
113+
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults
114+
to None
118115
:param telemetry_properties: Dict[str, str], optional
119-
:param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to None
116+
:param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to
117+
None
120118
:param telemetry_metrics: Dict[str, float], optional
121119
:return: The LUIS results of the analysis of the current message text in the current turn's context activity.
122120
:rtype: RecognizerResult
@@ -137,14 +135,15 @@ def on_recognizer_result(
137135
telemetry_metrics: Dict[str, float] = None,
138136
):
139137
"""Invoked prior to a LuisResult being logged.
140-
141138
:param recognizer_result: The Luis Results for the call.
142139
:type recognizer_result: RecognizerResult
143140
:param turn_context: Context object containing information for a single turn of conversation with a user.
144141
:type turn_context: TurnContext
145-
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults to None
142+
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults
143+
to None
146144
:param telemetry_properties: Dict[str, str], optional
147-
:param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to None
145+
:param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to
146+
None
148147
:param telemetry_metrics: Dict[str, float], optional
149148
"""
150149

@@ -159,7 +158,7 @@ def on_recognizer_result(
159158

< F438 /div>
160159
@staticmethod
161160
def _get_top_k_intent_score(
162-
intent_names: List[str], intents: Dict[str, IntentScore], index: int
161+
intent_names: List[str], intents: Dict[str, IntentScore], index: int # pylint: disable=unused-argument
163162
) -> Tuple[str, str]:
164163
intent_name = ""
165164
intent_score = "0.00"
@@ -178,14 +177,15 @@ def fill_luis_event_properties(
178177
) -> Dict[str, str]:
179178
"""Fills the event properties for LuisResult event for telemetry.
180179
These properties are logged when the recognizer is called.
181-
182180
:param recognizer_result: Last activity sent from user.
183181
:type recognizer_result: RecognizerResult
184182
:param turn_context: Context object containing information for a single turn of conversation with a user.
185183
:type turn_context: TurnContext
186-
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults to None
184+
:param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event,
185+
defaults to None
187186
:param telemetry_properties: Dict[str, str], optional
188-
:return: A dictionary that is sent as "Properties" to IBotTelemetryClient.TrackEvent method for the BotMessageSend event.
187+
:return: A dictionary that is sent as "Properties" to IBotTelemetryClient.TrackEvent method for the
188+
BotMessageSend event.
189189
:rtype: Dict[str, str]
190190
"""
191191

libraries/botbuilder-ai/botbuilder/ai/luis/luis_util.py

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
import platform
55
from collections import OrderedDict
6-
from typing import Dict, List, Set, Union
6+
from typing import Dict, List, Union
77

88
import azure.cognitiveservices.language.luis.runtime.models as runtime_models
99
from azure.cognitiveservices.language.luis.runtime.models import (
1010
CompositeEntityModel,
1111
EntityModel,
1212
LuisResult,
1313
)
14-
from botbuilder.core import IntentScore, RecognizerResult
1514
from msrest import Serializer
15+
from botbuilder.core import IntentScore, RecognizerResult
1616

1717
from .. import __title__, __version__
1818

@@ -35,12 +35,11 @@ def get_intents(luis_result: LuisResult) -> Dict[str, IntentScore]:
3535
LuisUtil.normalized_intent(i.intent): IntentScore(i.score or 0)
3636
for i in luis_result.intents
3737
}
38-
else:
39-
return {
40-
LuisUtil.normalized_intent(
41-
luis_result.top_scoring_intent.intent
42-
): IntentScore(luis_result.top_scoring_intent.score or 0)
43-
}
38+
return {
39+
LuisUtil.normalized_intent(
40+
luis_result.top_scoring_intent.intent
41+
): IntentScore(luis_result.top_scoring_intent.score or 0)
42+
}
4443

4544
@staticmethod
4645
def extract_entities_and_metadata(
@@ -58,9 +57,9 @@ def extract_entities_and_metadata(
5857
if composite_entities:
5958
composite_entity_types = set(ce.parent_type for ce in composite_entities)
6059
current = entities
61-
for compositeEntity in composite_entities:
60+
for composite_entity in composite_entities:
6261
current = LuisUtil.populate_composite_entity_model(
63-
compositeEntity, current, entities_and_metadata, verbose
62+
composite_entity, current, entities_and_metadata, verbose
6463
)
6564
entities = current
6665

@@ -90,12 +89,12 @@ def number(value: object) -> Union[int, float]:
9089
return None
9190

9291
try:
93-
s = str(value)
94-
i = int(s)
95-
return i
92+
str_value = str(value)
93+
int_value = int(str_value)
94+
return int_value
9695
except ValueError:
97-
f = float(s)
98-
return f
96+
float_value = float(str_value)
97+
return float_value
9998

10099
@staticmethod
101100
def extract_entity_value(entity: EntityModel) -> object:
@@ -108,7 +107,7 @@ def extract_entity_value(entity: EntityModel) -> object:
108107
resolution = entity.additional_properties["resolution"]
109108
if entity.type.startswith("builtin.datetime."):
110109
return resolution
111-
elif entity.type.startswith("builtin.datetimeV2."):
110+
if entity.type.startswith("builtin.datetimeV2."):
112111
if not resolution["values"]:
113112
return resolution
114113

@@ -117,32 +116,31 @@ def extract_entity_value(entity: EntityModel) -> object:
117116
timexes = [val["timex"] for val in resolution_values]
118117
distinct_timexes = list(OrderedDict.fromkeys(timexes))
119118
return {"type": val_type, "timex": distinct_timexes}
120-
else:
121-
if entity.type in {"builtin.number", "builtin.ordinal"}:
122-
return LuisUtil.number(resolution["value"])
123-
elif entity.type == "builtin.percentage":
124-
svalue = str(resolution["value"])
125-
if svalue.endswith("%"):
126-
svalue = svalue[:-1]
127-
128-
return LuisUtil.number(svalue)
129-
elif entity.type in {
130-
"builtin.age",
131-
"builtin.dimension",
132-
"builtin.currency",
133-
"builtin.temperature",
134-
}:
135-
units = resolution["unit"]
136-
val = LuisUtil.number(resolution["value"])
137-
obj = {}
138-
if val is not None:
139-
obj["number"] = val
140-
141-
obj["units"] = units
142-
return obj
143-
else:
144-
value = resolution.get("value")
145-
return value if value is not None else resolution.get("values")
119+
120+
if entity.type in {"builtin.number", "builtin.ordinal"}:
121+
return LuisUtil.number(resolution["value"])
122+
if entity.type == "builtin.percentage":
123+
svalue = str(resolution["value"])
124+
if svalue.endswith("%"):
125+
svalue = svalue[:-1]
126+
127+
return LuisUtil.number(svalue)
128+
if entity.type in {
129+
"builtin.age",
130+
"builtin.dimension",
131+
"builtin.currency",
132+
"builtin.temperature",
133+
}:
134+
units = resolution["unit"]
135+
val = LuisUtil.number(resolution["value"])
136+
obj = {}
137+
if val is not None:
138+
obj["number"] = val
139+
140+
obj["units"] = units
141+
return obj
142+
value = resolution.get("value")
143+
return value if value is not None else resolution.get("values")
146144

147145
@staticmethod
148146
def extract_entity_metadata(entity: EntityModel) -> Dict:
@@ -202,10 +200,10 @@ def populate_composite_entity_model(
202200
# This is now implemented as O(n^2) search and can be reduced to O(2n) using a map as an optimization if n grows
203201
composite_entity_metadata = next(
204202
(
205-
e
206-
for e in entities
207-
if e.type == composite_entity.parent_type
208-
and e.entity == composite_entity.value
203+
ent
204+
for ent in entities
205+
if ent.type == composite_entity.parent_type
206+
and ent.entity == composite_entity.value
209207
),
210208
None,
211209
)
@@ -310,7 +308,7 @@ def recognizer_result_as_dict(
310308
for name, intent_score in recognizer_result.intents.items()
311309
} if recognizer_result.intents is not None else None
312310

313-
d: Dict[str, object] = {
311+
dictionary: Dict[str, object] = {
314312
"text": recognizer_result.text,
315313
"alteredText": recognizer_result.altered_text,
316314
"intents": intents,
@@ -319,13 +317,13 @@ def recognizer_result_as_dict(
319317

320318
if recognizer_result.properties is not None:
321319
for key, value in recognizer_result.properties.items():
322-
if key not in d:
320+
if key not in dictionary:
323321
if isinstance(value, LuisResult):
324-
d[key] = LuisUtil.luis_result_as_dict(value)
322+
dictionary[key] = LuisUtil.luis_result_as_dict(value)
325323
else:
326-
d[key] = value
324+
dictionary[key] = value
327325

328-
return d
326+
return dictionary
329327

330328
@staticmethod
331329
def intent_score_as_dict(intent_score: IntentScore) -> Dict[str, float]:
@@ -343,5 +341,5 @@ def luis_result_as_dict(luis_result: LuisResult) -> Dict[str, object]:
343341
k: v for k, v in runtime_models.__dict__.items() if isinstance(v, type)
344342
}
345343
serializer = Serializer(client_models)
346-
d = serializer.body(luis_result, "LuisResult")
347-
return d
344+
result = serializer.body(luis_result, "LuisResult")
345+
return result

0 commit comments

Comments
 (0)
0