gbowne1 / json-maestro

JSONMaestro is a powerful tool designed for cleaning and processing JSON-like files. It simplifies tasks such as removing comments, eliminating duplicate keys, adding schema keys, and sorting keys. Ideal for developers working with configuration files and API responses, JSONMaestro enhances data integrity and prepares JSON data for further analysis
MIT License
2 stars 2 forks source link

Unnecessary isinstance check for Dict[str, Any] in add_schema_keys function #3

Closed gbowne1 closed 3 days ago

gbowne1 commented 4 weeks ago

In the add_schema_keys function, there's an unnecessary isinstance check that's causing a Pylance warning. The specific line is:

if isinstance(schema_item, dict):

Where schema_item is of type Dict[str, Any].

The warning message is: "Unnecessary isinstance call; "Dict[str, Any]" is always an instance of "dict[_KT@dict, _VT@dict]" PylancereportUnnecessaryIsInstance"

This check is redundant because schema_item is already known to be a dictionary type from its type annotation. The isinstance check doesn't provide any additional type safety in this context.

Proposed solution:

Remove the isinstance check and directly access the dictionary methods. The code can be simplified to:

schema_item.setdefault("fileMatch", [])
schema_item.setdefault("url", "")

This change will resolve the Pylance warning while maintaining the same functionality. Environment:

Python version: 3.7.3
Pylance version: Using the latest Pylance extension in VSCode for Linux

Additional context:

This issue was identified in a script for processing JSON and JSONC files, specifically in a function that adds schema-related keys to objects. I've tried a few things to fix this, but still have issues resolving this particular issue.