From a9fff24848f2c41a4c48bf083be257047740deee Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 9 Aug 2022 07:16:44 +0100 Subject: [PATCH] Explicitly specify encoding The update to pylint means we were failing because open() doesn't specify the encoding everywhere. I have now specified utf-8 everywhere. This was patchy before, and is default behaviour on the Pi. There's a slim chance it will cause some issues on Windows, but that shouldn't affect anyone outside the developers. I've also ignored a couple of new spurious warnings, and explicitly ignored some unused variables. --- openflexure_microscope/api/app.py | 4 ++-- openflexure_microscope/api/default_extensions/autofocus.py | 2 +- .../api/default_extensions/camera_stage_mapping.py | 2 +- openflexure_microscope/api/utilities/__init__.py | 2 +- openflexure_microscope/camera/base.py | 2 +- openflexure_microscope/captures/capture.py | 3 ++- openflexure_microscope/config.py | 6 +++--- openflexure_microscope/rescue/check_picamera.py | 2 +- openflexure_microscope/rescue/check_sangaboard.py | 2 +- scripts/zenodo/upload_to_zenodo.py | 4 ++-- tests/test_app.py | 2 +- 11 files changed, 16 insertions(+), 15 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 95908a51..ccd302bc 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -261,10 +261,10 @@ def generate_openapi(): if fname.endswith(".json"): import json - with open(fname, "w") as fd: + with open(fname, "w", encoding="utf-8") as fd: json.dump(labthing.spec.to_dict(), fd) else: - with open(fname, "w") as fd: + with open(fname, "w", encoding="utf-8") as fd: fd.write(labthing.spec.to_yaml()) diff --git a/openflexure_microscope/api/default_extensions/autofocus.py b/openflexure_microscope/api/default_extensions/autofocus.py index 04d98d3b..fa482dee 100644 --- a/openflexure_microscope/api/default_extensions/autofocus.py +++ b/openflexure_microscope/api/default_extensions/autofocus.py @@ -245,7 +245,7 @@ def extension_action(args=None): # Run the action return func(self.extension, **arguments) - def get(self, *args, **kwargs): # pylint: disable=useless-super-delegation + def get(self, *args, **kwargs): # pylint: disable=useless-super-delegation,arguments-differ # Explicitly wrap the `get` method to allow us to add a docstring return super().get(*args, **kwargs) diff --git a/openflexure_microscope/api/default_extensions/camera_stage_mapping.py b/openflexure_microscope/api/default_extensions/camera_stage_mapping.py index 032660ba..d87aad45 100644 --- a/openflexure_microscope/api/default_extensions/camera_stage_mapping.py +++ b/openflexure_microscope/api/default_extensions/camera_stage_mapping.py @@ -200,7 +200,7 @@ class CSMExtension(BaseExtension): "linear_calibration_y": cal_y, } - with open(CSM_DATAFILE_PATH, "w") as f: + with open(CSM_DATAFILE_PATH, "w", encoding="utf-8") as f: json.dump(data, f, cls=JSONEncoder) return data diff --git a/openflexure_microscope/api/utilities/__init__.py b/openflexure_microscope/api/utilities/__init__.py index 5460580a..c94c73f9 100644 --- a/openflexure_microscope/api/utilities/__init__.py +++ b/openflexure_microscope/api/utilities/__init__.py @@ -81,7 +81,7 @@ def init_default_extensions(extension_dir: str): create_file(default_ext_path) logging.info("Populating %s...", (default_ext_path)) - with open(default_ext_path, "w") as outfile: + with open(default_ext_path, "w", encoding="utf-8") as outfile: outfile.write(_DEFAULT_EXTENSION_INIT) diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index c7835b63..12f51946 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -54,7 +54,7 @@ class FrameStream(io.BytesIO): t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType], - ) -> Optional[bool]: + ) -> None: self.stop_tracking() return super().__exit__(t, value, traceback) diff --git a/openflexure_microscope/captures/capture.py b/openflexure_microscope/captures/capture.py index e4a5df68..1cd08a8f 100644 --- a/openflexure_microscope/captures/capture.py +++ b/openflexure_microscope/captures/capture.py @@ -120,7 +120,8 @@ class CaptureObject(object): logging.debug("Finished writing to disk %s", self.file) def open(self, mode): - return open(self.file, mode) + # We don't specify an encoding because this will be a binary file. + return open(self.file, mode) # pylint: disable=unspecified-encoding def split_file_path(self, filepath: str): """ diff --git a/openflexure_microscope/config.py b/openflexure_microscope/config.py index 5788a385..f7b9552f 100644 --- a/openflexure_microscope/config.py +++ b/openflexure_microscope/config.py @@ -88,7 +88,7 @@ def load_json_file(config_path) -> dict: logging.info("Loading %s...", config_path) - with open(config_path) as config_file: + with open(config_path, encoding="utf-8") as config_file: try: config_data = json.load(config_file) except json.decoder.JSONDecodeError as e: @@ -112,7 +112,7 @@ def save_json_file(config_path: str, config_dict: dict): logging.info("Saving %s...", config_path) logging.debug(config_dict) - with open(config_path, "w") as outfile: + with open(config_path, "w", encoding="utf-8") as outfile: json.dump(config_dict, outfile, cls=JSONEncoder, indent=2, sort_keys=True) @@ -150,7 +150,7 @@ def initialise_file(config_path, populate: str = "{}\n"): create_file(config_path) logging.info("Populating %s...", (config_path)) - with open(config_path, "w") as outfile: + with open(config_path, "w", encoding="utf-8") as outfile: outfile.write(populate) diff --git a/openflexure_microscope/rescue/check_picamera.py b/openflexure_microscope/rescue/check_picamera.py index 5490e13e..e8a5a113 100644 --- a/openflexure_microscope/rescue/check_picamera.py +++ b/openflexure_microscope/rescue/check_picamera.py @@ -26,7 +26,7 @@ def main(): try: import picamerax - except Exception as e: # pylint: disable=W0703 + except Exception as _: # pylint: disable=W0703 error_sources.append(PICAMERA_IMPORT_ERROR) else: try: diff --git a/openflexure_microscope/rescue/check_sangaboard.py b/openflexure_microscope/rescue/check_sangaboard.py index 13b154b1..e7b23b6b 100644 --- a/openflexure_microscope/rescue/check_sangaboard.py +++ b/openflexure_microscope/rescue/check_sangaboard.py @@ -20,7 +20,7 @@ def main(): # Try connecting on the specified port try: stage = Sangaboard(stage_port) - except FileNotFoundError as e: + except FileNotFoundError as _: if stage_port: error_sources.append( ErrorSource( diff --git a/scripts/zenodo/upload_to_zenodo.py b/scripts/zenodo/upload_to_zenodo.py index 244e63ce..aa30ce18 100644 --- a/scripts/zenodo/upload_to_zenodo.py +++ b/scripts/zenodo/upload_to_zenodo.py @@ -31,7 +31,7 @@ def script_directory(path): def get_meta(): - with open(script_directory("metadata.yaml")) as f: + with open(script_directory("metadata.yaml"), encoding="utf-8") as f: metadata = f.read() return yaml.safe_load(metadata) @@ -51,7 +51,7 @@ def main(): zenodo.upload_file(deposit["id"], path) link = deposit["links"]["latest_draft_html"] - with open("zenodo-link.html", "w") as f: + with open("zenodo-link.html", "w", encoding="utf-8") as f: f.write(f'{link}') diff --git a/tests/test_app.py b/tests/test_app.py index 0e16c58b..17c6076b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -28,7 +28,7 @@ def test_thing_description_valid(): # TODO: it would be nice to put this into LabThings # First load the schema from file and check it validates schema_fname = os.path.join(os.path.dirname(__file__), "w3c_td_schema.json") - schema = json.load(open(schema_fname, "r")) + schema = json.load(open(schema_fname, "r"), encoding="utf-8") jsonschema.Draft7Validator.check_schema(schema) # Build a TD dictionary