44import os
55import sys
66from importlib .abc import InspectLoader
7+ from pathlib import Path
78from types import ModuleType
89from typing import Any , Dict , Iterable , List , Mapping , Optional , TextIO , Union , cast
910
@@ -349,7 +350,7 @@ class FileConfiguration(Configuration):
349350
350351 def __init__ (
351352 self ,
352- data : Union [str , TextIO ],
353+ data : Union [str , Path , TextIO ],
353354 read_from_file : bool = False ,
354355 * ,
355356 lowercase_keys : bool = False ,
@@ -370,13 +371,15 @@ def __init__(
370371 interpolate = interpolate ,
371372 interpolate_type = interpolate_type ,
372373 )
373- self ._filename = data if read_from_file and isinstance (data , str ) else None
374+ self ._filename = (
375+ data if read_from_file and isinstance (data , (str , Path )) else None
376+ )
374377 self ._ignore_missing_paths = ignore_missing_paths
375378 self ._reload_with_check (data , read_from_file )
376379
377380 def _reload_with_check (
378381 self ,
379- data : Union [str , TextIO ],
382+ data : Union [str , Path , TextIO ],
380383 read_from_file : bool = False ,
381384 ) -> None : # pragma: no cover
382385 try :
@@ -388,7 +391,7 @@ def _reload_with_check(
388391
389392 def _reload (
390393 self ,
391- data : Union [str , TextIO ],
394+ data : Union [str , Path , TextIO ],
392395 read_from_file : bool = False ,
393396 ) -> None : # pragma: no cover
394397 raise NotImplementedError ()
@@ -404,12 +407,12 @@ class JSONConfiguration(FileConfiguration):
404407
405408 def _reload (
406409 self ,
407- data : Union [str , TextIO ],
410+ data : Union [str , Path , TextIO ],
408411 read_from_file : bool = False ,
409412 ) -> None :
410413 """Reload the JSON data."""
411414 if read_from_file :
412- if isinstance (data , str ):
415+ if isinstance (data , ( str , Path ) ):
413416 with open (data , "rt" ) as f :
414417 result = json .load (f )
415418 else :
@@ -420,7 +423,7 @@ def _reload(
420423
421424
422425def config_from_json (
423- data : Union [str , TextIO ],
426+ data : Union [str , Path , TextIO ],
424427 read_from_file : bool = False ,
425428 * ,
426429 lowercase_keys : bool = False ,
@@ -456,7 +459,7 @@ class INIConfiguration(FileConfiguration):
456459
457460 def __init__ (
458461 self ,
459- data : Union [str , TextIO ],
462+ data : Union [str , Path , TextIO ],
460463 read_from_file : bool = False ,
461464 * ,
462465 section_prefix : str = "" ,
@@ -476,7 +479,11 @@ def __init__(
476479 ignore_missing_paths = ignore_missing_paths ,
477480 )
478481
479- def _reload (self , data : Union [str , TextIO ], read_from_file : bool = False ) -> None :
482+ def _reload (
483+ self ,
484+ data : Union [str , Path , TextIO ],
485+ read_from_file : bool = False ,
486+ ) -> None :
480487 """Reload the INI data."""
481488 import configparser
482489
@@ -487,7 +494,7 @@ def optionxform(self, optionstr: str) -> str:
487494 return super ().optionxform (optionstr ) if lowercase else optionstr
488495
489496 if read_from_file :
490- if isinstance (data , str ):
497+ if isinstance (data , ( str , Path ) ):
491498 with open (data , "rt" ) as f :
492499 data = f .read ()
493500 else :
@@ -505,7 +512,7 @@ def optionxform(self, optionstr: str) -> str:
505512
506513
507514def config_from_ini (
508- data : Union [str , TextIO ],
515+ data : Union [str , Path , TextIO ],
509516 read_from_file : bool = False ,
510517 * ,
511518 section_prefix : str = "" ,
@@ -543,7 +550,7 @@ class DotEnvConfiguration(FileConfiguration):
543550
544551 def __init__ (
545552 self ,
546- data : Union [str , TextIO ],
553+ data : Union [str , Path , TextIO ],
547554 read_from_file : bool = False ,
548555 prefix : str = "" ,
549556 separator : str = "__" ,
@@ -567,12 +574,12 @@ def __init__(
567574
568575 def _reload (
569576 self ,
570- data : Union [str , TextIO ],
577+ data : Union [str , Path , TextIO ],
571578 read_from_file : bool = False ,
572579 ) -> None :
573580 """Reload the .env data."""
574581 if read_from_file :
575- if isinstance (data , str ):
582+ if isinstance (data , ( str , Path ) ):
576583 with open (data , "rt" ) as f :
577584 data = f .read ()
578585 else :
@@ -594,7 +601,7 @@ def _reload(
594601
595602
596603def config_from_dotenv (
597- data : Union [str , TextIO ],
604+ data : Union [str , Path , TextIO ],
598605 read_from_file : bool = False ,
599606 prefix : str = "" ,
600607 separator : str = "__" ,
@@ -634,7 +641,7 @@ class PythonConfiguration(Configuration):
634641
635642 def __init__ (
636643 self ,
637- module : Union [str , ModuleType ],
644+ module : Union [str , Path , ModuleType ],
638645 prefix : str = "" ,
639646 separator : str = "_" ,
640647 * ,
@@ -651,7 +658,8 @@ def __init__(
651658 lowercase_keys: whether to convert every key to lower case.
652659 """
653660 try :
654- if isinstance (module , str ):
661+ if isinstance (module , (str , Path )):
662+ module = str (module )
655663 if module .endswith (".py" ):
656664 import importlib .util
657665 from importlib import machinery
@@ -708,7 +716,7 @@ def reload(self) -> None:
708716
709717
710718def config_from_python (
711- module : Union [str , ModuleType ],
719+ module : Union [str , Path , ModuleType ],
712720 prefix : str = "" ,
713721 separator : str = "_" ,
714722 * ,
@@ -796,7 +804,7 @@ class YAMLConfiguration(FileConfiguration):
796804
797805 def __init__ (
798806 self ,
799- data : Union [str , TextIO ],
807+ data : Union [str , Path , TextIO ],
800808 read_from_file : bool = False ,
801809 * ,
802810 lowercase_keys : bool = False ,
@@ -818,9 +826,13 @@ def __init__(
818826 ignore_missing_paths = ignore_missing_paths ,
819827 )
820828
821- def _reload (self , data : Union [str , TextIO ], read_from_file : bool = False ) -> None :
829+ def _reload (
830+ self ,
831+ data : Union [str , Path , TextIO ],
832+ read_from_file : bool = False ,
833+ ) -> None :
822834 """Reload the YAML data."""
823- if read_from_file and isinstance (data , str ):
835+ if read_from_file and isinstance (data , ( str , Path ) ):
824836 with open (data , "rt" ) as f :
825837 loaded = yaml .load (f , Loader = yaml .FullLoader )
826838 else :
@@ -831,7 +843,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
831843
832844
833845def config_from_yaml (
834- data : Union [str , TextIO ],
846+ data : Union [str , Path , TextIO ],
835847 read_from_file : bool = False ,
836848 * ,
837849 lowercase_keys : bool = False ,
@@ -866,7 +878,7 @@ class TOMLConfiguration(FileConfiguration):
866878
867879 def __init__ (
868880 self ,
869- data : Union [str , TextIO ],
881+ data : Union [str , Path , TextIO ],
870882 read_from_file : bool = False ,
871883 * ,
872884 section_prefix : str = "" ,
@@ -891,10 +903,14 @@ def __init__(
891903 ignore_missing_paths = ignore_missing_paths ,
892904 )
893905
894- def _reload (self , data : Union [str , TextIO ], read_from_file : bool = False ) -> None :
906+ def _reload (
907+ self ,
908+ data : Union [str , Path , TextIO ],
909+ read_from_file : bool = False ,
910+ ) -> None :
895911 """Reload the TOML data."""
896912 if read_from_file :
897- if isinstance (data , str ):
913+ if isinstance (data , ( str , Path ) ):
898914 with open (data , "rb" ) as f :
899915 loaded = toml .load (f )
900916 else :
@@ -914,7 +930,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
914930
915931
916932def config_from_toml (
917- data : Union [str , TextIO ],
933+ data : Union [str , Path , TextIO ],
918934 read_from_file : bool = False ,
919935 * ,
920936 section_prefix : str = "" ,
0 commit comments