@@ -519,19 +519,16 @@ async fn init_project(
519
519
( requires_python, python_request)
520
520
} ;
521
521
522
- project_kind
523
- . init (
524
- name,
525
- path,
526
- & requires_python,
527
- python_request. as_ref ( ) ,
528
- vcs,
529
- build_backend,
530
- author_from,
531
- no_readme,
532
- package,
533
- )
534
- . await ?;
522
+ project_kind. init (
523
+ name,
524
+ path,
525
+ & requires_python,
526
+ vcs,
527
+ build_backend,
528
+ author_from,
529
+ no_readme,
530
+ package,
531
+ ) ?;
535
532
536
533
if let Some ( workspace) = workspace {
537
534
if workspace. excludes ( path) ? {
@@ -571,6 +568,40 @@ async fn init_project(
571
568
workspace. install_path( ) . simplified_display( ) . cyan( )
572
569
) ?;
573
570
}
571
+ // Write .python-version if it doesn't exist in the workspace or if the version differs
572
+ if let Some ( python_request) = python_request {
573
+ if PythonVersionFile :: discover ( path, & VersionFileDiscoveryOptions :: default ( ) )
574
+ . await ?
575
+ . filter ( |file| {
576
+ file. version ( )
577
+ . is_some_and ( |version| * version == python_request)
578
+ && file. path ( ) . parent ( ) . is_some_and ( |parent| {
579
+ parent == workspace. install_path ( ) || parent == path
580
+ } )
581
+ } )
582
+ . is_none ( )
583
+ {
584
+ PythonVersionFile :: new ( path. join ( ".python-version" ) )
585
+ . with_versions ( vec ! [ python_request. clone( ) ] )
586
+ . write ( )
587
+ . await ?;
588
+ }
589
+ }
590
+ } else {
591
+ // Write .python-version if it doesn't exist in the project directory.
592
+ if let Some ( python_request) = python_request {
593
+ if PythonVersionFile :: discover ( path, & VersionFileDiscoveryOptions :: default ( ) )
594
+ . await ?
595
+ . filter ( |file| file. version ( ) . is_some ( ) )
596
+ . filter ( |file| file. path ( ) . parent ( ) . is_some_and ( |parent| parent == path) )
597
+ . is_none ( )
598
+ {
599
+ PythonVersionFile :: new ( path. join ( ".python-version" ) )
600
+ . with_versions ( vec ! [ python_request. clone( ) ] )
601
+ . write ( )
602
+ . await ?;
603
+ }
604
+ }
574
605
}
575
606
576
607
Ok ( ( ) )
@@ -610,57 +641,46 @@ impl InitKind {
610
641
611
642
impl InitProjectKind {
612
643
/// Initialize this project kind at the target path.
613
- async fn init (
644
+ fn init (
614
645
self ,
615
646
name : & PackageName ,
616
647
path : & Path ,
617
648
requires_python : & RequiresPython ,
618
- python_request : Option < & PythonRequest > ,
619
649
vcs : Option < VersionControlSystem > ,
620
650
build_backend : Option < ProjectBuildBackend > ,
621
651
author_from : Option < AuthorFrom > ,
622
652
no_readme : bool ,
623
653
package : bool ,
624
654
) -> Result < ( ) > {
625
655
match self {
626
- InitProjectKind :: Application => {
627
- self . init_application (
628
- name,
629
- path,
630
- requires_python,
631
- python_request,
632
- vcs,
633
- build_backend,
634
- author_from,
635
- no_readme,
636
- package,
637
- )
638
- . await
639
- }
640
- InitProjectKind :: Library => {
641
- self . init_library (
642
- name,
643
- path,
644
- requires_python,
645
- python_request,
646
- vcs,
647
- build_backend,
648
- author_from,
649
- no_readme,
650
- package,
651
- )
652
- . await
653
- }
656
+ InitProjectKind :: Application => InitProjectKind :: init_application (
657
+ name,
658
+ path,
659
+ requires_python,
660
+ vcs,
661
+ build_backend,
662
+ author_from,
663
+ no_readme,
664
+ package,
665
+ ) ,
666
+ InitProjectKind :: Library => InitProjectKind :: init_library (
667
+ name,
668
+ path,
669
+ requires_python,
670
+ vcs,
671
+ build_backend,
672
+ author_from,
673
+ no_readme,
674
+ package,
675
+ ) ,
654
676
}
655
677
}
656
678
657
679
/// Initialize a Python application at the target path.
658
- async fn init_application (
659
- self ,
680
+ fn init_application (
660
681
name : & PackageName ,
661
682
path : & Path ,
662
683
requires_python : & RequiresPython ,
663
- python_request : Option < & PythonRequest > ,
664
684
vcs : Option < VersionControlSystem > ,
665
685
build_backend : Option < ProjectBuildBackend > ,
666
686
author_from : Option < AuthorFrom > ,
@@ -716,34 +736,17 @@ impl InitProjectKind {
716
736
}
717
737
fs_err:: write ( path. join ( "pyproject.toml" ) , pyproject) ?;
718
738
719
- // Write .python-version if it doesn't exist in the target or is empty.
720
- if let Some ( python_request) = python_request {
721
- if PythonVersionFile :: discover ( path, & VersionFileDiscoveryOptions :: default ( ) )
722
- . await ?
723
- . filter ( |file| file. version ( ) . is_some ( ) )
724
- . filter ( |file| file. path ( ) . parent ( ) . is_some_and ( |parent| parent == path) )
725
- . is_none ( )
726
- {
727
- PythonVersionFile :: new ( path. join ( ".python-version" ) )
728
- . with_versions ( vec ! [ python_request. clone( ) ] )
729
- . write ( )
730
- . await ?;
731
- }
732
- }
733
-
734
739
// Initialize the version control system.
735
740
init_vcs ( path, vcs) ?;
736
741
737
742
Ok ( ( ) )
738
743
}
739
744
740
745
/// Initialize a library project at the target path.
741
- async fn init_library (
742
- self ,
746
+ fn init_library (
743
747
name : & PackageName ,
744
748
path : & Path ,
745
749
requires_python : & RequiresPython ,
746
- python_request : Option < & PythonRequest > ,
747
750
vcs : Option < VersionControlSystem > ,
748
751
build_backend : Option < ProjectBuildBackend > ,
749
752
author_from : Option < AuthorFrom > ,
@@ -772,19 +775,6 @@ impl InitProjectKind {
772
775
// Generate `src` files
773
776
generate_package_scripts ( name, path, build_backend, true ) ?;
774
777
775
- // Write .python-version if it doesn't exist.
776
- if let Some ( python_request) = python_request {
777
- if PythonVersionFile :: discover ( path, & VersionFileDiscoveryOptions :: default ( ) )
778
- . await ?
779
- . is_none ( )
780
- {
781
- PythonVersionFile :: new ( path. join ( ".python-version" ) )
782
- . with_versions ( vec ! [ python_request. clone( ) ] )
783
- . write ( )
784
- . await ?;
785
- }
786
- }
787
-
788
778
// Initialize the version control system.
789
779
init_vcs ( path, vcs) ?;
790
780
0 commit comments